Germany
Offline
Edison Member
Karma: 28
Posts: 1583
|
 |
« Reply #15 on: August 02, 2012, 09:36:46 am » |
I knew machines (were they called computer in those days ?) with word sizes of 12 or 36 bit !
A byte being 8 bit is common sense, uint16_t is known by rather basic include files to "all" c compilers to get rid of that annoying uncertainly what an int is, when it matters.
But word = uint16_t and dword = uint32_t, might be something you do not need to follow, if you don't want to.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #16 on: August 02, 2012, 11:46:03 am » |
I do believe that the term word as used in computing originated before Gates was in business. It's size is not obvious, though. int, long, byte, float, double, all have easily remembered sizes. word is some vague typedef'd mess that never should have been allowed. It's far too vague.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 451
I am a amateur.
|
 |
« Reply #17 on: August 02, 2012, 11:57:34 am » |
A word is not shit. In programming terms, it is a group of bytes. On my Windows Programmer's Calculator, there is an option to choose the [signed] number size in bytes: 1) Byte (8 bits) 2) Word (16 bits) 3) Dword (32 bits) 4) Qword (64 bits) In Arduino, it is a short term for unsigned int. http://arduino.cc/en/Reference/Word. I like using words.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #18 on: August 02, 2012, 05:54:01 pm » |
I'm a bit more hardware oriented. Word length means a different thing to me. It was DEC who had the 12-bit words, either 4 too many or 4 too few.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 451
I am a amateur.
|
 |
« Reply #19 on: August 07, 2012, 09:00:04 pm » |
So, do you think that I should just give up on VectorString? Would it be better to have 3 String/string/VectorString objects, or 3 char arrays with 50 character space allocation each? I still want to know what is preventing my VectorString::getString() function from returning a string printable to the Serial Monitor.
|
|
|
|
|
Logged
|
|
|
|
|
Germany
Offline
Edison Member
Karma: 28
Posts: 1583
|
 |
« Reply #20 on: August 08, 2012, 02:31:56 am » |
Would it be better to have 3 String/string/VectorString objects, or 3 char arrays with 50 character space allocation each? If you need 3 texts, each max 50 characters wide, and you have the RAM space available, 3 char arrays definitely is the way to go. If during run time, one text shrinks considerably while another one grows, and you are low by about 50 bytes of RAM, I'd think about the worst cases and probably anything else isn't a solution either  I still want to know what is preventing my VectorString::getString() function from returning a string printable to the Serial Monitor. That's a valid reason not to give up.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #21 on: August 08, 2012, 05:25:52 am » |
I still want to know what is preventing my VectorString::getString() function from returning a string printable to the Serial Monitor. Are you referring to this code? char* VectorString::getString(){ const word vecSize = vec.size(); char buf[vecSize] = "\0"; for(word i = 0; i < vecSize; i++) buf[i] = vec[i]; return buf; } If so, it's already been explained why that does not work. You are returning a pointer to space on the heap. As soon as the function ends, that heap space is reclaimed, and your pointer now points to garbage.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #22 on: August 08, 2012, 05:36:07 am » |
You are returning a pointer to space on the heap heap sp. "stack"
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #23 on: August 08, 2012, 05:46:04 am » |
heap sp. "stack" OK. I'll try to keep them straight.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 451
I am a amateur.
|
 |
« Reply #24 on: August 09, 2012, 01:29:35 pm » |
Actually the function is now: char* VectorString::getString(){ const word vecSize = vec.size(); //char* buf; char buf[vecSize+1]; //buf = (char*)malloc(sizeof(char)*vecSize+1); for(word i = 0; i < vecSize; i++) buf[i] = vec[i]; buf[vecSize] = '\0'; return (char*)buf; } I forgot to change it. It still returns nothing.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #25 on: August 09, 2012, 01:33:14 pm » |
Actually the function is now: You haven't changed the fundamental fact that the array you are returning a pointer to is allocated on the stack. When the function ends, the stack is unwound, and there goes your data, down the drain. You could call malloc() to allocate space on the heap, instead, and return a pointer to that space. You need to be sure to free() it when done with it.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #26 on: August 09, 2012, 04:28:47 pm » |
Or you can allocate a buffer as part of the Vector class but WTH, you'll probably use a freaking String and then wonder why your sketch crashes later.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14099
Lua rocks!
|
 |
« Reply #27 on: August 09, 2012, 04:50:13 pm » |
You need to be sure to free() it when done with it.
You need to be sure that free() does not have bugs in it, which I am not sure of in the current IDE release.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 0
Posts: 451
I am a amateur.
|
 |
« Reply #28 on: August 09, 2012, 05:16:56 pm » |
I found an c++ example in a book that includes functions that return char arrays. I have omitted the irrelevant parts: class CArgNode { private: char szCommand[255], szParameter[255]; //... public: //... char * GetCommand(); }; char* CArgNode::GetCommand() { return (char*)this->szCommand; } A string is strcpy'd to szCommand in the constructor. One thing I notice that is different for my function is that the char array that it returns is a class variable. You could call malloc() to allocate space on the heap, instead, and return a pointer to that space. You need to be sure to free() it when done with it. How would I do that? Do I have to use free() outside of the function? If you need 3 texts, each max 50 characters wide, and you have the RAM space available, 3 char arrays definitely is the way to go. What if the length of the strings can be anywhere from 4 to 50 characters?
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #29 on: August 09, 2012, 06:55:02 pm » |
If you need 3 texts, each max 50 characters wide, and you have the RAM space available, 3 char arrays definitely is the way to go. What if the length of the strings can be anywhere from 4 to 50 characters? You make space for as many characters as the string might possibly get to, and for good engineering you control the length of the strings to what you need. Sometimes the thing to do is make one Big Buffer for all your strings and set a pointer for each within, but again you must control memory use especially when memory is very limited. Find a programming book that mentions that any more! It will likely be old or about MCU programming. Sorry but you're not in Kansas any more.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|