How to know if a pointer is valid?
-
Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
-
Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
Have you looked into
IsBadCodePtr()
,IsBadReadPtr()
,IsBadWritePtr()
, orIsBadStringPtr()
?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
-
Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
-
In Windows you can try
IsBadReadPtr
,IsBadWritePtr
andIsBadStringPtr
. In MFC you can tryAfxIsValidAddress
andAfxIsValidString
. In case of run-time library, you can try_CrtIsValidPointer
,_CrtIsValidHeapPointer
and_CrtIsMemoryBlock
.AfxIsValidAddress worked fine for me. Thanks a lots :)
-
Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
hatemtalbi wrote:
does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
When you ask that question something is wrong in your code. You should never have to check if a pointer is 'valid'.
-
Have you looked into
IsBadCodePtr()
,IsBadReadPtr()
,IsBadWritePtr()
, orIsBadStringPtr()
?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. In short if
IsBadReadPtr
returnsTRUE
you know for sure that the memory isn't safe to read; but if it returnsFALSE
it does ***NOT*** mean that the memory is safe to use - it could be on the heap's free list for example. These API are only meant for debugging purposes and not for memory management in the manner your post alludes to. Steve -
AfxIsValidAddress worked fine for me. Thanks a lots :)
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. For example: if
IsBadReadPtr
returnsTRUE
you know for sure that the memory isn't safe to read; but if it returnsFALSE
it does ***NOT*** mean that the memory is safe to read - it could be on the heap's free list for example. Most people don't believe when I tell them this. The following code shows this effect in action:int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );
If this practice was safe theASSERT
should fire: it doesn't. The memory is being cached in the heap but it is definitely ***NOT*** safe to use it. DO NOT USE THIS TECHNIQUE! Steve -
hatemtalbi wrote:
does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?
When you ask that question something is wrong in your code. You should never have to check if a pointer is 'valid'.
I agree 100%. You get a five for making so much sense. Steve
-
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. For example: if
IsBadReadPtr
returnsTRUE
you know for sure that the memory isn't safe to read; but if it returnsFALSE
it does ***NOT*** mean that the memory is safe to read - it could be on the heap's free list for example. Most people don't believe when I tell them this. The following code shows this effect in action:int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );
If this practice was safe theASSERT
should fire: it doesn't. The memory is being cached in the heap but it is definitely ***NOT*** safe to use it. DO NOT USE THIS TECHNIQUE! SteveStephen Hewitt wrote:
If this practice was safe the ASSERT should fire: it doesn't.
Actually, it should only fire if the address pointed to by
pInt
was not contained entirely within the program’s memory space.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. In short if
IsBadReadPtr
returnsTRUE
you know for sure that the memory isn't safe to read; but if it returnsFALSE
it does ***NOT*** mean that the memory is safe to use - it could be on the heap's free list for example. These API are only meant for debugging purposes and not for memory management in the manner your post alludes to. SteveStephen Hewitt wrote:
These API are only meant for debugging purposes and not for memory management in the manner your post alludes to.
Hence my question. :)
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
Stephen Hewitt wrote:
If this practice was safe the ASSERT should fire: it doesn't.
Actually, it should only fire if the address pointed to by
pInt
was not contained entirely within the program’s memory space.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
DavidCrow wrote:
Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.
Yes, that's what the API does. This doesn't contradict my statement. The OP wanted to know how to tell if memory "was allocated by the application and can be used without problem".
AfxIsValidAddress
was put forward as a solution and the OP accepted it claiming that it worked. I pointed out that if this API could be used to perform this function then theASSERT
in the code below should fire asAfxIsValidAddress
should returnFALSE
given that it is surely not safe to use a pointer after it has been deleted:int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );
This is not the case. If you try this codeAfxIsValidAddress
returnsTRUE
and thus theASSERT
will not fire. Why? Because the address pointed to bypInt
was entirely contained in the process’s address space. It's cached by the heap (the small block heap in this case) for recycling. So the problem is that just because an address is physically mapped into the process's address space doesn't mean it's safe to use. In general these APIs can only be used to tell if memory can't be safely used but not to tell if it is safe to use. Steve