473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointers manager ?

Hi !

I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).

As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).

Thanks a lot !!

Marcelo
Apr 20 '07 #1
12 1357
Hmmm....
Unfortunately C++ dont provide garbage collection...
U have to manually delete all your pointers ..whichever you have
allocated...

But there is good workaround for that...
Use Boost C++ library, you can use shared_ptr<>
Using that you d'nt have to worry for its deallocation...:)

On Apr 20, 8:54 pm, Marcelo Fernandez <marche_1...@hotmail.comwrote:
Hi !

I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).

As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).

Thanks a lot !!

Marcelo

Apr 20 '07 #2
Marcelo Fernandez wrote:
I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).

As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which
one should die (Java garbage collector fault...).
There are two approaches: either learn to manage their lifetime
or use a garbage collector. There are rules for managing lifetime
of dynamic objects, and I bet books talk about them (like ownership,
reference counting...), you just need to find the right book. As
for garbage collector, there are several implementations on the
market, just look for them.

There are no tools that can tell when the pointer "are suppose to be
dead". There are tools that can identify memory that has never been
deallocated after the program has finished running. Those are called
"memory leak detector". Look them up.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 20 '07 #3
On 2007-04-20 17:54, Marcelo Fernandez wrote:
Hi !

I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).
There are tools for such things, I don't know exactly how good they are
since I only used one or two on fairly simple programs. What I do
remember was that they took quite some time to use since they basically
run the program through a debugger and for each allocation on the heap
it recorded the memory address and line of code that made the allocation.

One such tool is called Valgrind, electrical fence is another.
As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).
A simpler way of doing things is usually to avoid using dynamic memory
unless necessary, and as you learn more C++ you'll see that it's not
necessary nearly as often as one might thing. In fact one can write
whole programs of quite some complexity without allocating memory on the
heap.

Since you have been using Java earlier you have gotten used to using new
whenever you create a variable, in C++ that is generally not the way to go.

Another useful feature of the C++ language is the auto_ptr and
shared_ptr (that one comes with boost I think) which work just like
pointers but they take care of the deallocation for you.

If you have a specific problem with some code you could post it here
(unless it's too large) and you can get some comments on it.

--
Erik Wikström
Apr 20 '07 #4
Marcelo Fernandez wrote:
As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).

Just in addition to what the others said: there is such a possibility,
but, as a beginner, in my opinion you should rather learn the
differences between Java and C++ In Java every Object is actually a
pointer, while in C++ this is not true, and you should really try to
make clean code that doesn't use too many pointers.

When you'll have confidence with the new language, you will learn how to
use (and how NOT to use) smart_pointers and such things!

Regards,

Zeppe
Apr 20 '07 #5
On Fri, 20 Apr 2007 08:54:08 -0700, Marcelo Fernandez wrote:
>I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).
The tool is your understanding of genuine C++ idioms.
>As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).
Look for RAII, eg. http://en.wikipedia.org/wiki/RAII
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 20 '07 #6
Erik Wikström wrote:
On 2007-04-20 17:54, Marcelo Fernandez wrote:
>Hi !

I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).

There are tools for such things, I don't know exactly how good they are
since I only used one or two on fairly simple programs. What I do
remember was that they took quite some time to use since they basically
run the program through a debugger and for each allocation on the heap
it recorded the memory address and line of code that made the allocation.

One such tool is called Valgrind, electrical fence is another.
Electric fence only detects use of deleted memory, not memory leaks IIRC.
>
>As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which
one should die (Java garbage collector fault...).

A simpler way of doing things is usually to avoid using dynamic memory
unless necessary, and as you learn more C++ you'll see that it's not
necessary nearly as often as one might thing. In fact one can write
whole programs of quite some complexity without allocating memory on the
heap.

Since you have been using Java earlier you have gotten used to using new
whenever you create a variable, in C++ that is generally not the way to go.

Another useful feature of the C++ language is the auto_ptr and
shared_ptr (that one comes with boost I think) which work just like
pointers but they take care of the deallocation for you.

If you have a specific problem with some code you could post it here
(unless it's too large) and you can get some comments on it.
Apr 20 '07 #7
Thank you guys

I will check for those tools, but I think I should really practice my
c++ programming. Thanks a lot!

Marcelo
Apr 20 '07 #8
On Apr 20, 5:54 pm, Marcelo Fernandez <marche_1...@hotmail.comwrote:
I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).
As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).
Try the Boehm collector. It can be configured to detect such
things. (Of course, it can also be configured to be a full
garbage collector, which is recommended in new projects. It's
only when you have to deal with legacy code that you wouldn't
use it.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 20 '07 #9
On Apr 20, 6:05 pm, anand chugh <anand.ch...@gmail.comwrote:
Hmmm....
Unfortunately C++ dont provide garbage collection...
It's not required, but there are third party garbage collectors
which work quite well with it.
U have to manually delete all your pointers ..whichever you have
allocated...
But there is good workaround for that...
Use Boost C++ library, you can use shared_ptr<>
Using that you d'nt have to worry for its deallocation...:)
It's not a silver bullet. For that matter, neither is garbage
collection; even in Java, you have to consider object lifetime
issues. Still, garbage collection simplifies the implementation
considerably; boost::shared_ptr, while less general, can also
simplify it in certain cases.

However, while I would never forego garbage collection in
production code if I have a choice, for a student, I'm not sure
that it is such a good idea. The lack of garbage collection
means more coding, but its presence doesn't eliminate the
necessity of doing the design steps---it may take more work to
explicitly delete the pointers, but if you are not doing it
correctly, it's probable that you had object lifetime issues in
our Java code as well, and that there were, in fact, subtle bugs
in it as well.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 20 '07 #10
On Apr 20, 6:07 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Marcelo Fernandez wrote:
I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).
As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which
one should die (Java garbage collector fault...).
There are two approaches: either learn to manage their lifetime
or use a garbage collector. There are rules for managing lifetime
of dynamic objects, and I bet books talk about them (like ownership,
reference counting...), you just need to find the right book. As
for garbage collector, there are several implementations on the
market, just look for them.
There are no tools that can tell when the pointer "are suppose to be
dead". There are tools that can identify memory that has never been
deallocated after the program has finished running. Those are called
"memory leak detector". Look them up.
Most such tools can also find dangling pointers---pointers to
memory that you have already freed. The two that I'm familiar
with are Purify (excellent, but definitly not priced for student
use), and valgrind (worked well the only time I used it, but
I've not enough experience with it to say more).

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 20 '07 #11
On Fri, 20 Apr 2007 14:11:10 -0700, Marcelo Fernandez wrote:
>I will check for those tools, but I think I should really practice my
c++ programming. Thanks a lot!
See also: Tom Cargill: Managing Dynamic Objects in C++
http://www.ddj.com/184409895
--
Roland Pibinger
"C++ is my favorite garbage collected language because
it produces so little garbage." - Bjarne Stroustrup
Apr 21 '07 #12
On 4ÔÂ20ÈÕ, ÏÂÎç11ʱ54·Ö, Marcelo Fernandez <marche_1...@hotmail.comwrote:
Hi !

I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).

As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).

Thanks a lot !!

Marcelo
You can use a excellent class called AutoFreeAlloc which is from WinX.
It has a perfect new and delete doing.
Also, It will autofree the memory you don't delete.

Apr 21 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
5
by: Boris | last post by:
I've a class C with a smart pointer (I use boost::shared_ptr) which is initialized in the constructor: class C { boost::shared_ptr<D> d; public: C() : d(new d()) { } }; When the program...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
31
by: Yevgen Muntyan | last post by:
6.2.4 of standard says: "The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime." Do I understand it right that value of pointer may or may...
6
by: JoeC | last post by:
I understand the basics of pointers, they point to memory locations. I would like to know resources for learning all about poters. I am having some problems erasing elements of pointers from a...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.