Checking bad pointers

Started by
25 comments, last by NineYearCycle 17 years, 9 months ago
Some pointer initialized would point to some address, and some un-initialized pointer would point to some bad address like 0xfdfdfdfd or 0xfefefefe etc. I want to check on run-time if a pointer has gone bad, while the only things that are checkable are:

if ( ptr == NULL ) // uninitialized
else
{
//ptr ok, proceed
}

this way, 0xfefefefe type bad pointers cannot be checked- so, is there a way to check that using ordinary pointers, and nothing fancy? Thank you.
Advertisement
Generally speaking, the easiest way to deal with this is to simply set your pointers to NULL to initialize them and after you deallocate them. Many people use a macro for the latter, but it's completely possible to do in a function:
template<typename T>void deallocate(T* x) {    delete x;    x = NULL;}


If you're trying to find memory leaks because of bad pointers, there's a large amount of utilities out that for finding them, most of them being free. Googling for "memory leak find" turned up an interesting article on flipcode which uses a few custom memory tracking functions. Another search turned up an interesting leak finding tool, with source. I also seem to remember there being a few functions in visual c++ for finding leaks, as well, but can't remember the name offhand and google doesn't turn up anything.
Quote:Original post by bytecoder
template<typename T>void deallocate(T* x) {    delete x;    x = NULL;}



This function wouldn't work as you would expect, for instance if you do:
int * x = new int (15);std::cout << "x == " << x << std::endl;deallocate(x);std::cout << "x == " << x << std::endl;

you would always see that in both cases x address is the same - NULL doesn't get assigned.
For the function to work, you'd have to pass the pointer by reference it like this:
template<typename T>void deallocate(T* &x) {    delete x;    x = NULL;}



Anyway, this function isn't very useful, because you'd need another one for array deallocation (delete[] operator). And you'd have to think carefuly because there would be problems if you'd confuse them.
Thank you for the links, they will come in handy; but what I was trying to ask (did not mention this before) was that suppose I had initialized pointer to NULL, then with course of time, due to some programming erros, the pointer starts pointing towards some incorrect address, now at the momement i cannot think of a good reason why this would happen, but things like that may happen- programmer's mistake, so would there be some way to check if a pointer has gone bad?

Thank you.
Quote:Original post by Taha Ansari
Thank you for the links, they will come in handy; but what I was trying to ask (did not mention this before) was that suppose I had initialized pointer to NULL, then with course of time, due to some programming erros, the pointer starts pointing towards some incorrect address, now at the momement i cannot think of a good reason why this would happen, but things like that may happen- programmer's mistake, so would there be some way to check if a pointer has gone bad?

Thank you.


If you have a pointer that points to NULL, then it can not magicaly start pointing to something else. If you make sure you always assign NULL to all your uninitialized and deallocated (deleted) pointers - you won't have any problems.

Also, if in some place your code expects some pointer to be (or not to be) NULL, you can place an assert to ensure that. Then even if some other coder will be using that code - he will immediately see what he should do before using your code or he will get an assertion error (after which he will change his code).

My suggestion - don't worry so much about it. ;)

Edit: if you are using MSVC, then this might be interesting to you. Good luck.
Quote:Original post by Taha Ansari
Thank you for the links, they will come in handy; but what I was trying to ask (did not mention this before) was that suppose I had initialized pointer to NULL, then with course of time, due to some programming erros, the pointer starts pointing towards some incorrect address, now at the momement i cannot think of a good reason why this would happen, but things like that may happen- programmer's mistake, so would there be some way to check if a pointer has gone bad?

Thank you.
NO!
At best you may be able to determine if a pointer points to a valid page or something, or with some OS specific stuff you can check if it points into the heap or into the stack. But there is no reliable way to determine that a pointer is valid, or pointing to what you expect. You can sometimes determine that a pointer is definately NOT valid, but you cannot really determine when it IS valid.

Unfortunately, it really is up to your program to get it right. There are many C++ tools to help you get it right though, that you need to be aware of, and learn to use. For example, smart-pointers of varying types.[google]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
lol, I get the picture..i guess

1) assert is a good debug option
2) other options on that msdn link are worth a try-looks useful
3) smart pointers should be tried, I will google for them..

All your posts have been really helpful.

Thank you.
Quote:Original post by Taha Ansari
3) smart pointers should be tried, I will google for them..


[boost]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by Taha Ansari
3) smart pointers should be tried, I will google for them..


[boost]


Isn't boost a big (30 Meg or so) library to be included...slowing things down a bit, or is it really worth it. I've heard too many people recommending it though...just dont like the idea of relying on other libraries....hmm.
About the "smart pointers":

Check Google on "auto pointer"(autoptr) and "reference counted pointers".
The first one is one that destructs the data when the program leaves the scope and the second one is one that deletes the data when no piece of code uses the pointer anymore, so you don't have to delete the memory yourself in both cases.
In both cases, the the template classes make sure that a value is NULL or holds valid data(under certain conditions).

Check the download page of my COW project for a sample of an AutoPtr. It's in "\Source\COW\AutoPtr.h".
If I recall it correctly, the standard C++ libraries(or was it STL?) also holds an autoptr class.
[www.LifeIsDigital.net - My open source projects and articles.

This topic is closed to new replies.

Advertisement