Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Validate a pointer? [duplicate]

Possible Duplicate:
Testing pointers for validity (C/C++)

If I have a pointer, like char* foo, is there any way to determine if foo points to a valid location in memory? (So, foo is not NULL, and delete has not yet been called on it.)

like image 502
Nick Heiner Avatar asked Nov 27 '22 23:11

Nick Heiner


2 Answers

No, there is not.

like image 95
James McNellis Avatar answered Dec 06 '22 08:12

James McNellis


No. It's pretty much your responsibility to make sure you don't use dangling pointers. However, one practice recommended by that link (I've seen some people use this, others hate it) is to immediately set the pointer to NULL after you free/delete it.

like image 33
dcp Avatar answered Dec 06 '22 07:12

dcp