Dynamic Memory Allocation

C

cplusplusquestion

When I use "new" and "delete", how can I check "delete" deallocated
all memory I've created using "new" for a very complicated program?
 
A

Alf P. Steinbach

* (e-mail address removed):
When I use "new" and "delete", how can I check "delete" deallocated
all memory I've created using "new" for a very complicated program?

Depends what you're really asking.

If you're really asking about how to /check/ that all you allocate is
deallocated, i.e. detecting memory leaks, then that is a tool usage question.
Some compilers provide special debug support[1]. There are also many tools that
work essentially as special purpose debuggers, such as ValGrind[2].

If you're asking about how to /ensure/ that all you allocate is deallocated, the
general answer in C++ is to use smart pointers and/or garbage collection. Take
a look at the Boost library's smart pointers, such as boost::shared_ptr[3]. Use
of garbage collection is still a bit on the experimental, requiring code
adaption, although as I understand it e.g. James Kanze (this example being my
only example! :) ) uses the Boehm garbage collector to good effect.


Cheers, & hth.,

- Alf


Notes:
[1] <url: http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx>
[2] <url: http://valgrind.org/>
[3] <url: http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/smart_ptr.htm>
 
C

cplusplusquestion

* (e-mail address removed):
When I use "new" and "delete", how can I check "delete" deallocated
all memory I've created using "new" for a very complicated program?

Depends what you're really asking.

If you're really asking about how to /check/ that all you allocate is
deallocated, i.e. detecting memory leaks, then that is a tool usage question.
Some compilers provide special debug support[1]. There are also many tools that
work essentially as special purpose debuggers, such as ValGrind[2].

If you're asking about how to /ensure/ that all you allocate is deallocated, the
general answer in C++ is to use smart pointers and/or garbage collection. Take
a look at the Boost library's smart pointers, such as boost::shared_ptr[3]. Use
of garbage collection is still a bit on the experimental, requiring code
adaption, although as I understand it e.g. James Kanze (this example being my
only example! :) ) uses the Boehm garbage collector to good effect.

Cheers, & hth.,

- Alf

Notes:
[1] <url:http://msdn2.microsoft.com/en-us/library/x98tx3cf(VS.80).aspx>
[2] <url:http://valgrind.org/>
[3] <url:http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/smart_ptr.htm>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Thanks.
 
J

Juha Nieminen

When I use "new" and "delete", how can I check "delete" deallocated
all memory I've created using "new" for a very complicated program?

You use a profiler program. In linux, for example, try valgrind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top