views:

1059

answers:

6

Hi, this question is related to this one. Given this code

char *p = new char[200];
delete[] p;

what would happen if you set p[100] = '\0' before deleting p?

I had some code where I got a debug error when I tried to delete a not null-terminated char array, something about deleting heap memory that's not assigned. It seemed to delete memory out of the arrays bounds.

EDIT: thx

+14  A: 

The code:

char *p = new char[200];
p[100] = '\0';
delete[] p;

is perfectly valid C++. delete does not know or care about null-terminated strings, so your error must have had some other cause.

anon
+2  A: 

It shouldn't matter. delete[] should be used to delete dynamically allocated array irrespective of its content.

aJ
+6  A: 

Nothing special would happen. You would write at some place in the middle of the allocated memory (100 bytes apart from the start, 99 bytes before the end of the allocated memory).

Then you would free that allocated memory. The compiler will handle it exactly as we would expect. The memory allocated by that is completely unrelated to null terminated strings. You could stick everything you want into that memory. It's some "raw" chunk of storage, you could even create some arbitrary C++ object into that memory (placement new).

Your bug is somewhere else. For example, some common error is this one, where the constructor is called once, but the destructor is called twice, double-deleting something:

struct A { P *p; A() { p = new P; } ~A() { delete p; } };
A getA() { return A(); } int main() { A a = getA(); }

Now, what happens is that the default constructor is called once, and the created object is copied zero or more times. But the destructor is run for each copy that's created. Thus, you will call the destructor on the pointer more than once, leading to such strange bugs. The correct way to fix that is to use a smart pointer, like shared_ptr. As an exercise, you can also do it without, by writing proper copy constructors that copy the object over and allocate memory in the copy constructor, so that the copy and the respective original object keep distinct pointers.

Johannes Schaub - litb
A: 

This should work just fine, as far as I can tell. You allocate a chunk of memory, and the OS should keep track of it, and be able to deallocate it when asked. It should not matter at all what values you put into the buffer you've allocated.

Sticking a NULL in the middle of a character array would certainly interfere with the C string functions such as strcmp, strlen, etc. but that is another matter entirely.

Dima
@Thomas http://en.wikipedia.org/wiki/Null_character Would it make you happy it I change "NULL" to "null"? :)
Dima
+3  A: 

I think you're confusing a plain old char array with a char array representing a C style string. The C++ delete operator cares nothing for a C style string array. All it will ever see is an array of chars. It's really no different than say deleting an array of int's.

The presence or absence of the null terminator is only relevant in functions that treat a char* as a C style string.

JaredPar
A: 

As others have noted, the code you've posted is perfectly valid and shouldn't cause any trouble.

The error might have been caused by changing the value of p somewhere in between.

Joey