PDA

View Full Version : C++ creating array of characters


brad211987
10-06-2009, 01:01 AM
Still don't have the memory model soundly in my head apparently. I am creating string as an array of characters in a C++ program:


char* myString = new char[8];
after this call, looking in a debugger shows that the array is padded with 8 encoded \253. This brings the length of my string to 16, can someone explain what the heck is going on here?

Here is a code snippet in question: it is a simple insert function, snipped out all but the first part where my issue is.


void MyString::insert(int pos1, const char* s)
{
char* org = storedValue;

int orgLength = strlen(org);
int insLength = strlen(s);

storedValue = new char[8];

int test = strlen(storedValue);
The 8 is a hard coded value for now, but it should be length of original string plus length of variable s, which for my test data happend to be 8. Anyway, at the end of this, the variable test gets the value of 16......why? Later I am adding characters to this new array(8 of them) and the extra padding creates strange characters on the final output.

**EDIT**
increasing my hard coded value to 16 results in an ending length of 24, so it's adding 8 bytes.....

**EDIT 2**
This seems to have something to do with windows vista and or eclipse....and/or MinGW. Copying the code into a cygwin installation seems to make the problem go away and start functioning normally.

oesxyl
10-06-2009, 01:05 AM
new only alocate memory, so you must init storedValue with some values.

best regards

brad211987
10-06-2009, 01:07 AM
Thats the part that had me confused, strlen should return 0 if there are no values, but instead gave me 16? It is behaving as expected now that I've removed it from windows though......still not sure im 100% comfortable with that.

oracleguy
10-06-2009, 01:17 AM
If you call strlen on a char* that you just newed, the results aren't reliable. strlen relies on the string being null terminated and will keep on counting until it finds a null. So it could go past the memory you allocated for the string.

And a word of warning with C & C++ debuggers. Depending on the debugger, some will initialize the program memory to zero, some will use a magic number (http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Magic_debug_values) and some will not do anything and you'll have whatever random data was in the memory at that location.

And usually this only happens if you build with debug symbols. That's how code can seem to work in debug but not in release, it usually means some variable or bit of memory was not properly initialized in the program.

oesxyl
10-06-2009, 01:19 AM
Thats the part that had me confused, strlen should return 0 if there are no values, but instead gave me 16? It is behaving as expected now that I've removed it from windows though......still not sure im 100% comfortable with that.
you don't have anything in that variable and strlen count how many chars are from the head of the array to the first \0, but the result is unpredictable.
new in this case only instruct the compiler to keep 8 bytes for that variable, that's all.

+2 seconds, :)
best regards

brad211987
10-06-2009, 04:03 AM
I think the debugger was throwing me for a loop. Thanks to both of you for filling in the gaps!