there are many utilites codes out there that use modified new/delete and smart pointers to do what you are asking. i suggest boost simply because it has something to do virtually everything.
there is no simply way of checking bad points. if you only wanted to check obviously bad pointers like 0 you could do that yourself. if you mean situations like possibly getting memory from new and later deleting, or having someone using your api not delete it, or going past the memory allocated by new then you are stuck writting code or using utilites.
the general idea is to check whether or not your new currently "owns" the memory at where the pointer points to.
aside from all that. it is better to write code that does not need this. do bounds checking use smart pointers that only delete themselves when the reference count reaches zero and write classes in such a way there are always in a valid state. that is no public data.
obag
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2004-09-23
C/C++ only checks for NULL (zero) pointer dereferencing. It assumes all non-zero pointers are valid. More sophisticated checking is not built into the language - follow Kip and obag's suggestions.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for you answers.
The problem is that I don't want do delete the objects the pointers point to. When I use an object corresponding to a pointer, I set it to NULL in order not to touch the object any more (via this list of pointers - I reuse the objects via another list). So, when I modify these pointers, I just set it to NULL ; if I don't modify them, they correctly point to the corresponding objects (I display the list). I never delete one of these pointers, that's why I'm astonished...
Paul
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
smart pointers are c++ classes that are used somewhat like pointers only they deal with themselves. usually through reference counting and various other techniques. for example the stds auto_ptr is used like this
auto_ptr<int> pt(new int);
and when the current block exits the destructor of auto_ptr takes care of calling delete.
i use various variants of the boost and loki smart pointers in my code. the general idea is instead of returning a block of memory like
someclass * f(data t1, data t2, int count)
{
someclass * a = new someclass[count];
// do word on a
return(a);
}
where the user of the function f would have to deal with deletion of the memory, or possibly cause an error, you could use
smart_pointer<someclass> f(data t1, data t2, int count)
{
smart_pointer<someclass> a(new someclass[count]);
// work with a as you normally would a ptr
// as a[0] or *(a + 1) or your preffered method
return(a); // destructor called but reference not 0
// nothing is deleted.
}
user code does not have to deal with deleting memory now. even looping through the function as
while(true)f(data1, data2, somenum);
would not cause a memory problem.
of course different smart pointer implementations use different methods. some do not do reference counting. some do not use * for dereferencing. and some do not support member functions like the standard auto pointer does. like get for example. some smart pointers have no member function just a constructor and a destructor and behave like the pointed to object. basically i am saying make sure you read the manual for the smart pointer you use.
from what i have seen of your code using some form of smart pointers would definately be more user friendly. i have seen the line
"user must call free"
or something to that effect many times.
obag
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
not to bring up the argument about your code again, but if you had taken my suggestions you would know all you need to know about smart pointers. the suggestion i refer to is the ones about read scott meyers and herb sutters book. and some time after you read those read modern c++ design by andrei alexadrescu. once again, do not read it until you are already very familiar with template code and how to use it.
You can check for a bad pointer before using the pointer with IsBadCodePtr, IsBadHugeReadPtr, IsBadHugeWritePtr, IsBadReadPtr, IsBadStringPtr, and IsBadWritePtr functions.
Otherwise, you can check for a bad pointer when accessed by writing your own exception handler. Once installed, it will be invoked when the OS throws a SIGSEGV (segment fault violation). You can use exception records and the various other structures provided to see where the fault was triggered, why, and more details such as what was accessed.
Kip
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi :)
Is there any way to prevent access errors on pointers like 0xffffffff or 0xbaadf00d ... ? I'm looking for something like a try/catch block ?
Thanks
Paul.
there are many utilites codes out there that use modified new/delete and smart pointers to do what you are asking. i suggest boost simply because it has something to do virtually everything.
there is no simply way of checking bad points. if you only wanted to check obviously bad pointers like 0 you could do that yourself. if you mean situations like possibly getting memory from new and later deleting, or having someone using your api not delete it, or going past the memory allocated by new then you are stuck writting code or using utilites.
the general idea is to check whether or not your new currently "owns" the memory at where the pointer points to.
aside from all that. it is better to write code that does not need this. do bounds checking use smart pointers that only delete themselves when the reference count reaches zero and write classes in such a way there are always in a valid state. that is no public data.
obag
C/C++ only checks for NULL (zero) pointer dereferencing. It assumes all non-zero pointers are valid. More sophisticated checking is not built into the language - follow Kip and obag's suggestions.
Clifford
Cliff / Obag, can you tell me more about pointer dereferencing?
Kip
I sense I am being had, but if it helps:
dereferencing: accessing the object pointed to by a pointer. It the pointer is zero, the runtime can detect it as an error.
Clifford
I think what Paul wants is something like SEH: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/structured_exception_handling.asp
last time I checked, MinGW didn't support it
Adrian
Thanks for you answers.
The problem is that I don't want do delete the objects the pointers point to. When I use an object corresponding to a pointer, I set it to NULL in order not to touch the object any more (via this list of pointers - I reuse the objects via another list). So, when I modify these pointers, I just set it to NULL ; if I don't modify them, they correctly point to the corresponding objects (I display the list). I never delete one of these pointers, that's why I'm astonished...
Paul
Woops, sorry, I asked the wrong question. I meant smart pointers =)
Kip
smart pointers are c++ classes that are used somewhat like pointers only they deal with themselves. usually through reference counting and various other techniques. for example the stds auto_ptr is used like this
auto_ptr<int> pt(new int);
and when the current block exits the destructor of auto_ptr takes care of calling delete.
see here
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
i use various variants of the boost and loki smart pointers in my code. the general idea is instead of returning a block of memory like
someclass * f(data t1, data t2, int count)
{
someclass * a = new someclass[count];
// do word on a
return(a);
}
where the user of the function f would have to deal with deletion of the memory, or possibly cause an error, you could use
smart_pointer<someclass> f(data t1, data t2, int count)
{
smart_pointer<someclass> a(new someclass[count]);
// work with a as you normally would a ptr
// as a[0] or *(a + 1) or your preffered method
return(a); // destructor called but reference not 0
// nothing is deleted.
}
user code does not have to deal with deleting memory now. even looping through the function as
while(true)f(data1, data2, somenum);
would not cause a memory problem.
of course different smart pointer implementations use different methods. some do not do reference counting. some do not use * for dereferencing. and some do not support member functions like the standard auto pointer does. like get for example. some smart pointers have no member function just a constructor and a destructor and behave like the pointed to object. basically i am saying make sure you read the manual for the smart pointer you use.
from what i have seen of your code using some form of smart pointers would definately be more user friendly. i have seen the line
"user must call free"
or something to that effect many times.
obag
not to bring up the argument about your code again, but if you had taken my suggestions you would know all you need to know about smart pointers. the suggestion i refer to is the ones about read scott meyers and herb sutters book. and some time after you read those read modern c++ design by andrei alexadrescu. once again, do not read it until you are already very familiar with template code and how to use it.
a better link to std::auto_ptr:
http://www.roguewave.com/support/docs/sourcepro/stdlibref/auto-ptr.html
the books i am talking about:
http://www.amazon.com/exec/obidos/ASIN/0201749629/qid=1096126092/sr=ka-1/ref=pd_ka_1/103-0727029-5327864
http://www.amazon.com/exec/obidos/ASIN/0201924889/qid=1096126115/sr=ka-1/ref=pd_ka_1/103-0727029-5327864
http://www.amazon.com/exec/obidos/ASIN/020163371X/qid=1096126068/sr=ka-2/ref=pd_ka_2/103-0727029-5327864
http://www.amazon.com/exec/obidos/ASIN/0201615622/qid=1096126192/sr=ka-2/ref=pd_ka_2/103-0727029-5327864
http://www.amazon.com/exec/obidos/ASIN/020170434X/qid=1096126216/sr=ka-2/ref=pd_ka_2/103-0727029-5327864
http://www.amazon.com/exec/obidos/ASIN/0201704315/qid=1096126230/sr=ka-1/ref=pd_ka_1/103-0727029-5327864
obag
Thanks Obag. Will do =)
Kip
You can check for a bad pointer before using the pointer with IsBadCodePtr, IsBadHugeReadPtr, IsBadHugeWritePtr, IsBadReadPtr, IsBadStringPtr, and IsBadWritePtr functions.
Otherwise, you can check for a bad pointer when accessed by writing your own exception handler. Once installed, it will be invoked when the OS throws a SIGSEGV (segment fault violation). You can use exception records and the various other structures provided to see where the fault was triggered, why, and more details such as what was accessed.
Kip