Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi.

I want to list/display all pointers used in a file. Please tell me how to do this.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jun-12 13:56pm    
Why?
--SA
Albert Holguin 27-Jun-12 14:00pm    
All pointers used in a file? ...not sure I understand your question.
OriginalGriff 27-Jun-12 14:04pm    
What kind of file?
What kind of pointers?
Malli_S 28-Jun-12 5:27am    
Is that you are parsing the cpp file?
siddalinga.hiremath 30-Jun-12 11:06am    
Yes. I need to parse cpp file and list all the pointer variables to see if any memory leaks.

Not clear why would you need this, but here is my note on it: the problem cannot have a strict solution, because it is not well defined.

First issue is that a C++ file is not a functional unit (not a class, not an executable or something like that), but just some text. From an isolated file, it's not always possible to recognize if some variable or a function parameter is a pointer or not. The more general problem is that the pointer is essentially a run-time notion. Strictly speaking, a pointer cannot be defined as an artifact of the text of some program code. Two different variables can present the pointer to the same object; would you list them separately or not? Inversely, during run time a set of pointer does not have to correspond to some names or any artifacts of the code, so how to list them? This is something strictly impossible to tell what it is during compile time. Take a regular linked list. It is all linked with the pointers, these pointers are distinctly different but appears only during run time.

And don't even mention the possibility of prediction of run time based on the text of the code — this is strictly impossible in general case. If you don't understand it or want to learn about it, please start from reading about the important halting problem, and the fundamental theorem about it, the fundamental theorem of computability theory:
http://en.wikipedia.org/wiki/Halting_problem[^],
http://en.wikipedia.org/wiki/Computability_theory_%28computer_science%29[^].

Are you getting the idea? As this is the ill-posed problem, it should not be solved. And I don't think you can "invent" any strict formulation of the problem or any formulation which would make sense.

—SA
 
Share this answer
 
Let me try to rephrase your question. Are you looking for all symbols in a source file that denote a pointer to something? This would in fact be valid question.

In the good old times we had so called cross-reference generators that produced a listing of all program variables, sorted by name or by type and with all their occurrences in a set of source files. I think that would at least help you quite a bit. If that fits your needs, just google under "C++ cross-reference" and you will find quite a few interesting tools.

If you are trying to find all pointers to solve a memory leak or dangling pointer problem I would like to point you in another direction as there are far better ways to approach those problems. You find those under "memory leak detection" on this website.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jun-12 12:07pm    
It does not answer the question, but only because the problem in question is not well defined by OP. But these are useful considerations, even though they are based on your guesswork. I would like to give more clear idea to OP: look for "memory debuggers". If the OP's problem is related to the memory leak problem, of course, which we cannot be 100% sure. (I voted 4 for the answer.)
--SA
Sergey Alexandrovich Kryukov 28-Jun-12 12:11pm    
I added one more answer along the line you have suggested, with a useful reference, credited your answer. Please see.
--SA
nv3 28-Jun-12 13:58pm    
Thanks for the valuable hint, Sergey!
Sergey Alexandrovich Kryukov 29-Jun-12 18:18pm    
My pleasure.
--SA
siddalinga.hiremath 30-Jun-12 11:10am    
@nv3..Yes..you are absolutely correct.I am trying to find all pointers to solve memory leaks.Before that i wanted to list pointer variables from a cpp file. Later I can take each pointer and identify possible memory leaks or dangling pointer..Sorry for the short problem description.
To clarify the advice given by nv3 in Solution 2:

If the problem is somehow related to the problem of memory leaks, as nv3 suggested, please see this article on "memory debugger": http://en.wikipedia.org/wiki/Memory_debugger[^].

Look at the list of the memory debuggers found in this article. Some tools are Open-Source ones.

—SA
 
Share this answer
 
Comments
siddalinga.hiremath 30-Jun-12 11:11am    
@SA..Thank you.. I will look into this document and get back if any doubts.
Thanks
Sergey Alexandrovich Kryukov 30-Jun-12 13:07pm    
Sure, and please accept this answer formally (green button) -- thanks.
--SA

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