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.
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.
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.
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.
std::shared_ptr<int> pointer(newint);
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.