1

Hi my problem is kind of difficult to explain so I'll just post my code section here and explain the problem with an example.

This code here has a big and a small array where the big array gets split up in small parts, is stored in the small array and the small array is outputting its content on the screen.
Afterwards I free the allocated memory of the small array and initialize it again with the next part of the big array:

//this code is in a loop that runs until all of the big array has been copied
char* splitArray = new char[50];        
strncpy(splitArray, bigArray+startPoint, 50); //startPoint is calculated with every loop run, it marks the next point in the array for copying

//output of splitArray on the screen here

delete splitArray;
//repeat loop here

now my problem is that the outputted string has everytime some random symbols at the end. for example "some_characters_here...last_char_hereRANDOM_CHARS_HERE".

after looking deeper into it I found out that splitArray actually doesnt have a size of 50 but of 64 with the null terminator at 64. so when I copy from bigArray into splitArray then there are still the 14 random characters left after the real string and of course I dont want to output them.

A simple solution would be to manually set the null terminator in the splitArray at [50] but then the program fails to delete the array again.

Can anybody help me find a solution for this? Preferably with some example code, thanks.

user1175111
  • 219
  • 3
  • 15
  • 2
    That should be `delete[]`, not `delete`. This almost certainly would cause memory-related problems. – Jon Feb 15 '12 at 12:27
  • One thing: `delete splitArray;` should be `delete[] splitArray;` – Bojan Komazec Feb 15 '12 at 12:29
  • wow so much answers here in this short time, I dont even know which one to answer but thank you all very much ;) Is it better to use string in C++? because I need a char* to output the array. and why is it better to use delete[] when I initialize the array like this? A link to some explanation would be enough but you can also explain here if you want – user1175111 Feb 15 '12 at 12:54

5 Answers5

0

How does the program "fail to delete the array again" if you just set splitArray[49] = 0? Don't forget, an array of length 50 is indexed from 0 through 49. splitArray[50] = 0 is writing to memory outside that allocated for splitArray, with all the consequences that entails.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • Although C++ accepts it, I find it quite misleading to mix `0` and `'\0'`. They are conceptually fundamentally different things. – Konrad Rudolph Feb 15 '12 at 12:32
  • it was an off by one error, I initialized the array with [50] but also wrote the null terminator to [50] and not [49] as it should be. so after increasing the array size +1 and writing the null terminator to [50] it works just fine, thanks – user1175111 Feb 15 '12 at 12:56
0

When you allocate memory for the splitArray the memory is not filled with NULL characters, you need to explictly do it. Because of this your string is not properly NULL terminated. To do this you can do char* splitArray = new char[51](); to initialize with NULL character at the time of allocation itself (note that I am allocating 51 chars to have the extra NULL character at the end). . Also note that you need to do delete[] splitArray; and not delete splitArray;.

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

Just doing this will be sufficient:

char* splitArray = new char[50 + 1];        
strncpy(splitArray, bigArray+startPoint, 50);
splitArray[50] = '\0';

I'd really question why you're doing this anyway though. This is much cleaner:

std::string split(bigArray+startPoint, 50);

it still does the copy, but handles (de)allocation and termination for you. You can get the underlying character pointer like so:

char const *s = split.c_str();

it'll be correctly nul-terminated, and have the same lifetime as the string object (ie, you don't need to free or delete it).


NB. I haven't changed your original code, but losing the magic integer literals would also be a good idea.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • well so far I didnt work much with string in c++. I need to pass a char* to output the content, I assume there is also a conversation possible from string to char*? – user1175111 Feb 15 '12 at 13:07
  • If you're using C++ anyway it's worth finding out what facilities you get free in the standard library, instead of reinventing everything. – Useless Feb 15 '12 at 13:10
0

The function strncpy has the disadvantage that it doesn't terminate the destination string, if the source string contains more than 50 chars. Seems like it does in your case!

If this really is C++, you can do it with std::string splitArray(bigArray+startPoint, 50).

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

I see a couple of problems with your code:

  1. If you allocate by using new [], you need to free with delete [] (not delete)
  2. Why are you using freestore anyway? From what I can see you might as well use local array.
  3. If you want to store 50 characters in an array, you need 51 for the terminating null character.

You wanted some code:

while(/* condition */)
{
    // your logic

    char splitArray[51];
    strncpy(splitArray, bigArray+startPoint, 50);
    splitArray[50] = '\0';

    // do stuff with splitArray
    // no delete
}
Fiktik
  • 1,941
  • 16
  • 25
  • doh stupid me, I forgot the zero based index of the array. So I set splitArray[50] but also initialized splitArray with splitArray[50] and not [50+1]. this was just some example code by me, I dont intialize the array with a constant value. and from what I can tell delete[] has the same effect as delete but I already heard that I should use it for such code. could you explain why? – user1175111 Feb 15 '12 at 12:51
  • @user1175111: See this question: http://stackoverflow.com/questions/2425728/delete-vs-delete-operators-in-c – Naveen Feb 15 '12 at 13:08
  • or could it be that this is automatically handeled by the compiler? because in the debugger I really cant see any difference when I use delete[] or delete and never had problems with delete and arrays so far. I am using Visual Studio 2010 btw. – user1175111 Feb 15 '12 at 13:09
  • It might be handled correctly in the debug build, but not in the release build. It might leak memory. It might do some other nastier things. I don't know what happens in this situation on VC++10, but even if everything works fine for you now, it can come back and bite you later when you change some harmless compiler setting or some totally unrelated condition is suddenly different. Just don't do it. There is a reason why `delete []` exists. – Fiktik Feb 15 '12 at 13:30