944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1851
  • C++ RSS
Oct 7th, 2009
0

Deleted memory still accessible?

Expand Post »
Hello. This problem occured in a program that used dynamic memory, and the arrays were deleted and reallocated several times with varying length. It seemed to me that after deleting the arrays, the pointer is still pointing to some data with the same length. (And of course i didn't want it.) So i made a little program to see what delete[] does. I could not only read, but even write the deleted memory. (???)

Here is the test program:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main () {
  5.  
  6. int* a;
  7. a = new int[30];
  8.  
  9. char pause;
  10.  
  11. for (int i=0; i<30; i++) a[i]=i;
  12. cout << a[3] << '\n';
  13.  
  14. delete[] a;
  15. cout << a[3] << '\n';
  16.  
  17. a[3]=9;
  18. cout << a[3];
  19.  
  20. cin >> pause;
  21.  
  22. return 0; }

The program creates an array of 30 integers, and fills it with numbers. The array is then deleted by callling the delete[] operator. Finally i change the value of the already deleted data. The program writes the original value, the value after deletion and the value after modification.

The result: 3, -17891602 and 9 at debugging. ( 3 is the original value. At debug, when deleting an array, its content is automatically replaced by these -17891602 characters. 9 is the changed value.) At least after debug, i get a heap corruption error.

When i run the program without debugging: 3, 3 and 9. I get no errors. No exceptions, crash, an error message, nothing, like it's perfectly correct to modify unallocated memory.

The question: could anybody explain how exactly delete[] works, why doesn't it make the memory inaccessible and how i can prevent the program from accidentally using resources that already have been freed? (I use visual c++ 2008.)

Thank you!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dqddani is offline Offline
1 posts
since Oct 2009
Oct 7th, 2009
0
Re: Deleted memory still accessible?
well one good practice to get into is to set your pointer to null after you delete it
c++ Syntax (Toggle Plain Text)
  1. int * a = new int[30];
  2. delete [] a;
  3. a = NULL;
  4. //or
  5. a = 0;
this will make sure the the pointer no longer points to where it used to and will also make it safe to delete again. with the delete function if you call it on a pointer you have already deleted than it will cause an error but calling delete is guaranteed to be safe. the delete function just frees the memory back up to the system. it doesn't actually delete the information in the memory.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Oct 7th, 2009
-6
Re: Deleted memory still accessible?
wrong but I thought that may be Delete doesn't delete the data in terms of zeroing it. Instead, it flags the memory as deleted. The actual memory still holds data, the memory manager just knows now that it can overwrite it with new stuff.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Could not deduce template argument...
Next Thread in C++ Forum Timeline: While loop Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC