4

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.)

Community
  • 1
  • 1
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • 3
    you could set it to `NULL` when you call `delete`. – João Portela Jun 17 '10 at 18:01
  • 8
    Best is to get rid of the pointer in the first place. Put it in a smart pointer that will be usable the entire duration of its lifetime, then the question is trivially "yes". If you can't do that, then get rid of the opportunity to possibly have an invalid pointer. Don't set it to `NULL` when you `delete`, get rid of the pointer! – GManNickG Jun 17 '10 at 18:02
  • 1
    Setting a pointer to `NULL` (or any other method of "getting rid of it") doesn't handle any aliases that the pointer may have; the only correct answer is "no". – Tyler McHenry Jun 17 '10 at 18:07
  • 2
    Dupe of http://stackoverflow.com/questions/551069/testing-pointers-for-validity-c-c among others. –  Jun 17 '10 at 18:09
  • @Tyler: GMan was suggesting the use of smart pointers, which have overloaded copy and assignment constructors, so aliases are not an issue. In that case, "getting rid of it" mean letting it go out of scope and does address the issue correctly. – Vagrant Jun 17 '10 at 18:14
  • Well everybody says no. And I do agree for "[any] valid location in memory". But there is no reason why checking if the pointer actually points to a valid address on the heap should be impossible. Some memory allocation library quite certainly provides this functionality. – log0 Jun 17 '10 at 18:28

6 Answers6

15

No, there is not.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

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.

dcp
  • 54,410
  • 22
  • 144
  • 164
3

Not in any implementation-independent manner.

If one knew the implementation details of the heap manager, one could walk the heap blocks and verify that the pointer is a valid heap block.

But that would be slow and (extremely) implementation dependent; it most likely would not work across compiler versions, much less across different compilers on the same OS, and certainly not across different OSes.

It's far better to ensure that the pointer is either valid or NULL, using something like RAII or smart pointers.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
2

You can't determine whether foo points to a valid location, but there are plenty of things you can do to ensure such is the case (especially RAII).

Cogwheel
  • 22,781
  • 4
  • 49
  • 67
1

If you could determine such a thing, that would mean there was extra bloat and overhead that would not be required 99.999% of the time and that runs counter to the principles of C++.

If you are worried about this sort of thing then use boost::shared_ptr.

Vagrant
  • 1,696
  • 12
  • 19
0

This is not pretty, I wouldn't do it as I prefer to use smart pointers, but here is some older style code.

valid location

I saw an interesting definition of valid once. If you can access it - say by using it, then it's valid. You can catch the exception raised when accessing it fails, thus you can define that it is valid or not. Valid in this case meaning 'accessible' but not defining anything about type.

valid_ptr( void * validate ) {
  try {
     int foo = *(int *)validate;
     return true;
  }
  catch(...){}
  return false;
}

This garauntees the memory is currently readable by your app, it does not mean your type specific pointer is pointing at an object of that type.

Perhaps a newer version of this method would also use a dynamic_cast or such to verify the type aswell trying to use RTTI information.

Greg Domjan
  • 13,943
  • 6
  • 43
  • 59