Click here to Skip to main content
15,878,602 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written a code to detect the following three conditions :
1. use of new without delete.
2. using "new int;" kind of declaration...in other words address not taken of memory allocated.
3. int*p = new int [10];
delete []p;
Can anyone help me and suggest various possible syntax for memory allocations in c++ so that i can make my code more robust.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 12:12pm    
And where?
--SA

1. Use of new without delete
Some scenarios where this is intended:

  • Functions returning pointer to allocated memory. Calling function must free the memory.
  • Functions assigning pointer to parameter (by pointer or reference). Calling function must free the memory.
  • Functions assigning pointer to object parameter (class/struct member). Calling function must free the memory.
  • Pointer is stored in class member and freed somewhere else (e.g. in destructor). The delete instruction may be in another source file in this case.


See the Run-Time Library Reference for the syntax of the scalar new operator and the vector new [] operator. See also the C++ Language Reference for the new operator. With objects, optional construction parameters may be passed:
C++
CMyObject o2 = new CMyObject();
CMyObject o2 = new CMyObject(p1, p2);


Other memory allocation functions are:

  • malloc, see the Run-Time Library Reference about other CRT functions that call malloc. Some of them require freeing allocated memory (e.g. _getcwd).
  • calloc

  • LocalAlloc
  • GlobalAlloc, code is often freed elsewhere.
  • HeapAlloc
  • VirtualAlloc
  • Various Windows SDK functions that allocate memory.
 
Share this answer
 
The problem isn't so much finding occurences of new and delete, but finding out which of those are paired. Destruction may not happen in the same function as construction, so you're bound to find many false positives if all you do is check for new without delete within a function.

Also you should take special care of the placement new operator, which on first sight looks like a memory allocation, but in truth does no allocation at all.

That said, there are plenty of tools out there that already do such static analysis. Why don't you just search a bit - I'm sure you can find some open source projects too.
 
Share this answer
 
Don't forget malloc/realloc/calloc and free.

Don't forget polymorphism.
Don't forget pointer ownership ( T* p = new T;T* p1 = p; delete p1; ) either intra/extra functions.


I found this http://suif.stanford.edu/~dlheine/thesis.pdf[^] that is interseting albeit quite academic.
 
Share this answer
 
Thanx everyone for you help.
@SAKryukov - Be a little mature dude...thats all i can say to you ! If you cannot help atleast dont disrespect :) and yea save all this sarcasm for your family Dude! show it to them!

Cheers!
 
Share this answer
 
You did not share your code or any information on the techniques you have developed, so why do you hope that someone shared improvements of these techniques with you? Especially after reading such weird expressions like "various possible syntax for memory allocations "…

You can learn more on this field if you look through the list available memory debuggers: http://en.wikipedia.org/wiki/Memory_debugger[^].

Some of them are Open Source, so you can look at the code and see how it works.

—SA
 
Share this answer
 

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