Check for deallocated memory?

Jul 20, 2014 at 8:59am
How would I go about checking for deallocated memory?

For example, let's take this into consideration:
1
2
3
4
// Unsigned 32-bit / 64-bit integer: uint32, uint64
uint32* Pointer = new uint32[ Size ];
uint64  MemAddr = ( uint64 ) Pointer;
delete[] Pointer;

The above code would proceed to create a new array, store it in a pointer and retrieve the memory address of the array before finally deleting the array.

So let's assume we re-build the pointer and try to access the now deallocated array:
1
2
Pointer = ( uint32* ) MemAddr;
Pointer[ 0 ] = 0;

Based on the above snippets of code, how would I check "Pointer" after rebuilding the memory to check if the rebuilt memory has actually been deallocated. Without a check we'd get an exception error.

A bit of detail on why I am trying this:
Before thinking up how to do this, I was storing the addresses in a list and check the list for the addresses to see if they existed or not. However this requires an O(n) search, which isn't exactly what I am wanting. So instead if I used a check for deallocation method, I can go for an O(1) time check and reduce the total time it would take to check for memory allocation/deallocation.
Last edited on Jul 20, 2014 at 11:10pm
Jul 20, 2014 at 9:40am
When you deallocate memory, also set the pointer to NULL (use some macro which you always use - > one for simple delete and one for arrays). To check just compare the pointer with NULL and you are done.
Jul 20, 2014 at 10:04am
I should have specified more. This suggestion doesn't work.

Currently every time I store a new array I store it's memory address into a list. This is used to make multiple checks as well as store the memory address.

Only one pointer is always used(outside of very rare temporary pointers in a few functions). So setting this one pointer to NULL happens when an array is deleted and the pointer points to the deleted array, but later this pointer can be used to store another array as needed. So all arrays are stored as memory addresses until they're actually going to be used. So I never actually have a pointer allocated for each array.

So checking for NULL in a pointer only helps an array when the main pointer points to that array, not for all of the dynamically allocated arrays.
Last edited on Jul 20, 2014 at 10:06am
Jul 20, 2014 at 10:24am
Currently every time I store a new array I store it's memory address into a list.

Therefore, every time you deallocate, you must also remove corresponding entry from the list.
Jul 20, 2014 at 10:31am
I know and that happens.

The problem is that even if the entry is deleted, I have to search the list to check and see if the memory is deallocated to check if the memory "exists."

This causes an O(n) check due to having to loop through the list until a comparison is found or not.

If I can dynamically check allocation using only the provided memory address instead of a list search, I could change the situation to an O(1) operation rather than an O(n) operation.
Jul 20, 2014 at 11:25am
Then there are the smart pointers.
Jul 20, 2014 at 8:52pm
Smart pointers? What functionality do those provide?
Jul 20, 2014 at 9:36pm
Jul 21, 2014 at 12:33am
Hmm... I don't see how that'd help at all.
Mind enlightening me?
Jul 21, 2014 at 1:00am
closed account (10X9216C)
1
2
3
4
5
6
7
8
9
10
11
std::shared_ptr<int> pointer(new int);

std::weak_ptr<int> memAddr = pointer;

pointer.reset(); // effectively, deallocates

if(auto ptr = memAddr.lock())
{
    // valid
    // isn't run in this case as was deallocated
}


Have to be careful as shared_ptr doesn't have support for arrays like unique_ptr<T[]> does.
Last edited on Jul 21, 2014 at 1:05am
Jul 21, 2014 at 1:22am
+1 @ smart pointers. If you are doing manual memory allocation/deallocation, you are doing it very wrong.
Jul 21, 2014 at 2:11am
So I thought. I'll consider this.
Topic archived. No new replies allowed.