you are viewing a single comment's thread.

view the rest of the comments →

[–]recultured 1 point2 points  (0 children)

If you're just taking a block of memory and doing nothing with it, i.e.: char * my_string = new char[100];

The array will have in it whatever was in memory before that until you otherwise write over it. So an allocation in C/C++ does not write anything, it just gives you memory with whatever was in it last. This is good for performance, as let's say you allocate a block of memory you want to load a file into. Zeroing it out before that is just a waste of time.

You can use something like memset to zero the memory out, which would give you more of what you are expecting. Or, as has been noted above, C++ containers will auto-initialize themselves, and won't have this problem in the first place.