Pages: 1 [2] 3   Go Down
Author Topic: Vector char array as string  (Read 3179 times)
0 Members and 1 Guest are viewing this topic.
Germany
Online Online
Faraday Member
**
Karma: 49
Posts: 2754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 in your IDE.

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Online Online
Faraday Member
**
Karma: 49
Posts: 2754
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 smiley-wink

Quote
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 Offline
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
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 Offline
Brattain Member
*****
Karma: 240
Posts: 24462
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
heap sp. "stack"
OK. I'll try to keep them straight.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually the function is now:
Code:
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 Offline
Brattain Member
*****
Karma: 551
Posts: 46265
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 in your IDE.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I found an c++ example in a book that includes functions that return char arrays. I have omitted the irrelevant parts:
Code:
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.

Quote from: PaulS
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?

Quote from: michael_x
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 Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: michael_x
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 in your IDE.

Pages: 1 [2] 3   Go Up
Jump to: