0

I have a class property which is an array of strings (std::string command[10]). When I assign some string value to it, it stop the program execution. As you can see below I've a string variable tempCommandStr which I assign to my property. I don't know what the error could be, but I've the print statement after assignment which is never executed, while the one preceding it is.

//Declared in class header
std::string command[10];

// Part of function which is causing problem.
string tempCommandStr(commandCharArray);
printf("%s\n", tempCommandStr.c_str()); // Prints fine.
this->command[i] = tempCommandStr; // Something goes wrong here. i is set to some correct value, i.e. not out of range.
printf("%s\n", this->command[i].c_str()); // Never prints. Also program stops responding.

// I noticed that getting any value from the array also stops the execution.
// Just the following statement would stop the program too.
printf("%s\n", this->command[i].c_str());

It's not just this property, I also have another array which has the same problem. What could be causing this? What's actually going wrong (look at edit)? Is there another better way to do this?

I'm running the program on an MBED so I've limited debugging options.

EDIT: I found the problem, I was cleaning the array before using to remove any previous values by memset(command, 0, sizeof(command));. This is was causing the problem. Now I use the clear function on each item in array as following. This fixed the execution problem.

for (int i = 0; i < sizeof(command)/sizeof(*command); i++){
     command[i].clear();
 }

Question: Why does setting the string array to 0 using memset makes it unusable?

artless noise
  • 21,212
  • 6
  • 68
  • 105
user14492
  • 2,116
  • 3
  • 25
  • 44
  • 2
    Can you copy the code to a platform where you can experiment with it? Can you prepare a [minimal complete example](http://stackoverflow.com/help/mcve)? – Beta Nov 22 '15 at 21:03
  • 1. can you check the value of `command[i].c_str()`? 2. what happened if you use `cout` instead of `printf`? – SHR Nov 22 '15 at 21:09
  • BTW, you don't need to use the `this->` notation, refer to data members directly. – Thomas Matthews Nov 22 '15 at 21:19
  • are you sure `i` is initialized? – Andrey Starodubtsev Nov 23 '15 at 16:46
  • The class is clearly going to have some degree of internal state to keep track of its storage, at the very least - it's not too great a surprise that destroying that state would make things go wrong. [This seems like a suitable jumping-off point](http://stackoverflow.com/q/1466073/3156750). – Notlikethat Nov 23 '15 at 17:16
  • `I'm running the program on an MBED so I've limited debugging options.` Then build it and run it on a normal machine. Many (most?) programming errors will manifest *wherever* you run the program. –  Nov 24 '15 at 14:39

2 Answers2

2

Why does setting the string array to 0 using memset makes it unusable?

Because you're obliterating the values held in the string class, overwriting them all with 0s. A std::string has pointers to the memory where it's storing the string, character count information, etc. If you memset() all that to 0, it's not going to work.

Rob K
  • 8,757
  • 2
  • 32
  • 36
0

You're coming from the wrong default position. Types where 'zeroing out' the memory is a meaningful (or even useful) operation are special; you should not expect any good to come from doing such a thing unless the type was specifically designed to work with that.