Click here to Skip to main content
15,888,079 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

Will be there be any memory leak if memory is dynamically allocated for an array using new[] operator and is dealloctaed using delete instead of delete[].

Example:
int* p = new [10];
delete p;


Will it give any memory leak?

If there is any meory leak in this case, how to detect it? I have tried using _CrtDumpMemoryLeaks(), but did not get any help whether there is any momory leak or not in this case.


Thanks,
Dev.
Posted
Updated 1-Jun-12 5:12am
v2
Comments
Albert Holguin 1-Jun-12 11:12am    
Don't forget, because it doesn't leak in one compiler doesn't mean it won't leak in another. MS doesn't always follow standards very closely.

According to the C++ standard the behavior is undefined if you delete an array with the single-instance delete operator. Thus, it is always desirable to use the correct one -- that much you probably knew already.

In the present Microsoft VC++ implementation nothing tragical in terms of memory allocation happens if you use the "wrong" delete, as the allocated memory chunks always have a hidden length field in their headers and the runtime always frees the correct amount of memory. That is, however, not guaranteed in the future.

BUT: Another story are the destructors that need to be called. As long as your array consists of plain-old-data (for example an array of int) again nothing bad happens. If your data type has however a non-trivial destructor you will notice the difference. For a single-instance delete only one object is being destroyed, while delete [] calls the destructor on all objects of the array. That makes a BIG difference and in fact memory leaks may result as a consequence if your destructor has to deallocate further objects.
 
Share this answer
 
Comments
BillW33 5-Jun-12 9:47am    
Good answer, +5
nv3 5-Jun-12 10:02am    
Thanks!
As nv3 said, don't do it - the compiler could generate code to do just about anything.

Anyway, rather than worrying about using delete or delete[] why not take the easy, incredibly safe, way out and use std::unique_ptr, std::shared_ptr or std::vector to manage memory for you?

So if you want an array of things that have a default constructor you can use:
C++
std::vector<my_class>( 20 );
to create a vector of 20 objects of class my_class . And you won't have to worry about whether you need to call delete or delete[]. And you can do just about anything you did with a built in array - including indexing past the end if you feel so careless.

So Ash's advice for pain free memory leak detection in C++ is:

- don't manually manage memory
- use stack based/automatic objects wherever you can
- where you can't use them use std::vector, std::unique_ptr or std::shared_ptr instead

Do that lot and you can spend the rest of your programming life with C++ not leaking any memory, never double deleting objects and having all the other "fun" you can have with pointers.
 
Share this answer
 
v2
Comments
Aescleal 1-Jun-12 16:02pm    
Hello person that gave me a two - could you please explain why you scored me the way you did? This isn't because I wish to go and hit all your posts by voting them down but because I want to do three things:

- Correct any factual inaccuracies
- Improve the communication and presentation of things I've said to make them more accessible
- Have a record of why people disagree with me so other people can see what the points of contention are and make up their own mind.

In all cases I want to learn, so please give me some clues about what I'm missing!
BillW33 5-Jun-12 9:48am    
Good advice, +5 from me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900