   validateing pointers
  how do you check to see if a pointer is validi.e. before you free/delete one, to make sure you can   Bobboau, bringing you products that work... in theory 
  As a rule of thumb, check it against NULL.You should be very sure to make any unallocated pointer NULL, and make any pointer NULL after it's free'd. There's no particularly good way to check if a pointer is to an invalid memory address other than NULL (by definition, 0x00000000).   Now, there is one way to do this, but it's an extremely bad idea, you should never rely on it, and in fact, it's not guaranteed to work properly in any given case, not to mention it will fail miserably on OSes that do not properly protect memory. bool IsValidPointer( void* p ){    int i;    try    {   i = (int) *p    }    catch    {   //access violation, must not be valid   return false;    }    //nothing happened, so it might be ok.    return true;}But like I said, don't rely on this.    [edited by - Promit on February 16, 2004 7:12:04 PM];  well I''m trying to make a last line of defence type memory freer, becase a large number of srtdups were used (not by me, or anyone else currently working on the project) in a project I''m working on without freeing them, the problem is that on rare ocasion they did so my inital atack of simply writeing an object to track useage of this function and free every allocation it made doesn''t work, unless I can tell if the memory address in question has been freed, wich 999 times out of 1000 it hasn''t been, but that 1 time causes... problems, and if I could just try to check the memory befor I free it everything will work ;  damn forgot to hit that keep me loged in button   Bobboau, bringing you products that work... in theory ;  What''s an srtdup?In any case, I think you should override or hide the original memory allocation functions (either operator new or malloc depending on language I guess) and then use a custom memory allocator, that keeps track of allocated memory and uses garbage collection to delete stuff that''s not used any more. ;  ah, that should have been strdup.string duplicate, I was thinking it was STL, maybe it isn''t (had never seen it untill we just found all these memory leaks comehting from it), the nameing just made me think it was I guess. basicly its a malloc and a strcpy, you give it a string and it will return a pointer to one just like it, but at a diferent location.we''re thinking about makeing a new memory management system   Bobboau, bringing you products that work... in theory ;  try/catch will not properly catch an exception that comes from an illegal pointer dereference, because that would require the compiler to emit stack unwind information for each failure point of each instruction (there could be more than one failure point in a single instruction).On UNIX, you will get a signal if you read a bad address. You can catch this signal and longjmp() out to a previous setjmp(), if you feel adventurous. This can be used to detect whether reading a pointer fails or not.On Microsoft OSes with Microsoft compilers, you can use "structured exception handling" which is basically another way of dealing with signals. You use __try and __except (rather than try/catch). This can also be used to detect whether the pointer can be read.Once you can "safely" read a pointer, it remains to determine whether it''s safe to delete/free it. Note that NULL can always safely be free()-ed and delete-ed and delete[]-ed and ::operator delete()-ed -- it''s in the standard! One way of doing this is to override your allocation functions, and put a magic value right before the memory block when allocating, and then check for that value when deallocating (and set it to something else, to avoid double frees).Another way means keeping an array or linked list of all allocations you''ve made, and validate the pointer against this list.Note that strdup() is a standard C function, just like strcpy(). It''s probably used by some library you''re linking with.Here''s a simple example:void * operator new( size_t size ) {  size += sizeof( long );  long * l = (long *)malloc( size );  *l = 0x12345678;  return (void *)&l[1];}void operator delete( void * ptr ) {  long * l = (long *)ptr;  if( !safe_to_read( l-1 ) ) {    assert( !"bad delete" );    return;  }  if( l[-1] != 0x12345678 ) {    assert( !"bad delete" );    return;  }  l[-1] = 0x87654321;  free( l-1 );}  ;  You might not like this answer, but the best way is to never run into the situation where you have a pointer you don''t know whether it should be deleted or not.There are several ways to help you to get there. Prefer references instead of pointers. Keep data on the stack whenever possible. Encapsulate places where you use pointers, so you have one single point of allocation and one single point of deletion.Having a large code base with NULL-checks before every delete is really messy. Don''t go there. ;  Promit... What kind of abomination is that? In standard C++, dereferencing an invalid pointer is a one-way ticket to UndefinedBehaviourLand. You definitely don''t get an exception.Now, Windows'' SEH is another issue... “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 (C programming language co-inventor);quote:Original post by Bobboau  string duplicate, I was thinking it was STL, maybe it isn''t (had never seen it untill we just found all these memory leaks comehting from it)=strdup() is non-standard C. It is indeed basically a malloc plus a strcpy. The memory must be deallocated with free().   “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 (C programming language co-inventor)