Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programs & Techniques for finding when a Pointer goes out of scope

I am a Software intern working on a large project in C++, & I am also a One man team. Daunting (is that how u spell it :P), yes it is, but also fun.

I am running into c++ pointers that end up pointing to NULL (not because the memory is freed/deleted but because, well I dont really know, but I suspect its because the objects I point to are updated, thus the originally assigned pointer doesn't point to the correct memory address.

As you can see I am confused myself, which is why I want to know of techniques or free programs that assist me in determining WHEN & WHERE a pointer points to null or falls out of scope (dangling pointers? is that whats happening).

Maybe I can just use a simple ASSERT call to determine when a pointer is null?

More info: Right now my problem is that I have a class that has member variables that are pointers. They are initialised in the constructor & by the end of the constructor those pointers points to correct objects. But in reactions to events, when I look at these pointers they are null/out of scope. I can determine this because 1. the program crashes & 2 the eclipse debugger shows them as red hollow circles (I think that means that point to NULL or are out of scope?).

like image 998
Mack Avatar asked Jan 25 '26 08:01

Mack


1 Answers

I would suggest two different tools:

  • Cppcheck is a static analyser that looks at your source code and tries to identify problems (with pointers, and with a lot of other things too).

  • Valgrind is a runtime tool that watches all the allocations and deallocations your program performs, and ensures that they are all correct.

like image 103
Greg Hewgill Avatar answered Jan 26 '26 22:01

Greg Hewgill