If I want to dynamically create an int array in C++ and then delete it later, do I need to null terminate the int array? If so, how do I do it? If not, how does the computer know how much memory to free?
int *array = new int[x];
..do some stuff..
delete array;
I know for a char array it deletes up to a null character ('\0') and if you don't have a terminating null character in your string you'll probably get memory violation errors but how does this apply to arrays of ints (and other arrays, like arrays of structs)?
The block of memory used by your array has some extra data in there, that isn't directly visible to your program, that tells delete how big the block is.
What you say about char arrays is not true, by the way - the presence of a NUL terminator isn't relevant to delete - it uses the hidden block size just like any other block of memory.
(Incidentally, you should say delete [] array; when you're deleting an array.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With