Hi
Please look at this small piece of code:

char* pArray = new someArray[128];
delete pArray;
deleta[] pArray;

Will this generate a mem leak?
"delete pArray" wil deallocate only the first char of someArray but will "delete[] pArray" deallocate the rest of someArray or will the compiler see it as already deallocated because the first char i missing?

The code example is only for demonstration and not some of my real code:icon_lol:

/Jacob

Recommended Answers

All 7 Replies

I generates undefined behavior. new goes with delete and new[] goes with delete[] , you can't mix them. But in practice it will cause a memory leak for the first delete, and probably also a crash when you try to delete the second time.

Not probably a crash but it will definitely crash the second time.Because the first delete will deallocate the memory reference with respect to pArray then second time when we try to deallocate the memory references we try for pArray[0] to pArray[9] but parray[0] is already de allocated by first statement.
Just use the second statement thats enough.

How come you are posting a problem about new and delete in a C forum ??? Its C++ forum you should post it in.

I have just modified the code and corrected an error to make it more clear and :

char* pArray;
pArray = new char[128];
delete pArray;
deleta[] pArray;

c++ textbook says it's fine to allocate arrays using "new".
You say it will create undefined behaviour, but I second that, I see no reason for undefined behaviour os please explain.

Not probably a crash but it will definitely crash the second time.Because the first delete will deallocate the memory reference with respect to pArray then second time when we try to deallocate the memory references we try for pArray[0] to pArray[9] but parray[0] is already de allocated by first statement.
Just use the second statement thats enough.

How come you are posting a problem about new and delete in a C forum ??? Its C++ forum you should post it in.

Tests show that i will not crash so that is not correct. The question is if it will create mem leak?
(Sorry about posting in the C forum but I can't move it, maybe a moderator can move the post?)

Well it crashed when I tried it.Ya it does create a mem leak if not a crash.

Well we don't have a problem with new the problem is with delete.Read the answers again we have already mentioned the correction.You can check this link for better info.

c++ textbook says it's fine to allocate arrays using "new".

Yeah, that's fine. What's not fine is allocating an array using new[] , then freeing it using delete :

int *p = new int[10]; // ok
delete p; // undefined. need to use delete[]

int *p = new int; // ok
delete[] p // undefined. need to use delete

int *p = new int[10]; // ok
delete p; // undefined. need to use delete[]. probably a memory leak
delete[] p; // undefined. calling delete twice

Tests show that i will not crash so that is not correct.

Testing only shows what will happen for that run. Undefined behavior can work fine one time, crash another, and send rude emails to your boss another, all without changing anything. Instead of asking what will happen, just don't invoke UB.

Yeah, that's fine. What's not fine is allocating an array using new[] , then freeing it using delete :

int *p = new int[10]; // ok
delete p; // undefined. need to use delete[]

int *p = new int; // ok
delete[] p // undefined. need to use delete

int *p = new int[10]; // ok
delete p; // undefined. need to use delete[]. probably a memory leak
delete[] p; // undefined. calling delete twice

Testing only shows what will happen for that run. Undefined behavior can work fine one time, crash another, and send rude emails to your boss another, all without changing anything. Instead of asking what will happen, just don't invoke UB.

I know deallocating an array using "delete" is a problem and should always be done using "delete[]" but I'm just trying to find out how dangerous it is. It just became clear to me that it can definitely result in a crash!
Fx. if another thread allocates data in the area of pArray[0] just at the point in time between the first and second delete, then the newly allocated data from the other thread will get deallocated by the second delete. ...not good :S

The reason I asked the question in the first place is that I found this type of construction in some old code, running on a lot of embedded devices and it worried me. I guess there is nothing to do but correct this one.

Thanks for your help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.