Deleted memory still accessible?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 1
Reputation: dqddani is an unknown quantity at this point 
Solved Threads: 0
dqddani dqddani is offline Offline
Newbie Poster

Deleted memory still accessible?

 
0
  #1
Oct 7th, 2009
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:

  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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 7th, 2009
well one good practice to get into is to set your pointer to null after you delete it
  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.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator
 
-6
  #3
Oct 7th, 2009
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.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Reply

Tags
delete, dynamic, memory

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 732 | Replies: 2
Thread Tools Search this Thread



Tag cloud for delete, dynamic, memory
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC