|
-
December 17th, 2010, 10:33 AM
#5
Re: does delete clear the memory?
 Originally Posted by felix1432
Is there any way of re-allocating the "deleted" memory & restoring the data?
Would it make sense to ZeroMemory() the secret buffer before deleting it?
This is very much dependent on your compiler and architecture. The long and the short of it is if you can point to that memory, and your system lets you read it, and it hasn't been written over, then yes; it can be read again.
Here's an example.
Code:
char* p_firstChar = new char;
char* p_secondChar = new char;
// These two chars could have been allocated in consecutive memory locations
p_secondChar = X; // Your secret letter
delete p_secondChar;
p_firstChar++; // p_firstChar now points one character beyond where it originally pointed.
// This could be pointing at the memory allocated for p_secondChar.
char secretLetter = *p_firstChar; // Read the contents of where p_secondChar used to point.
// Compiler/system dependent, you could be reading "X"
The standard dictates the following:
Delete calls the objects destructor (note that destructors are a bit special for primitives like char).
Delete call a deallocation function.
That deallocation function in turn "shall deallocate the storage referenced by the pointer". The standard specifies that the memory is then "reclaimed".
This seems to indicate that if your destructor doesn't trash the memory, nothing else can be relied on to either. If you want to ensure it does, I suggest you add a destructor to your object that writes over the top of it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|