| | |
Deleted memory still accessible?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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:
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!
Here is the test program:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int* a; a = new int[30]; char pause; for (int i=0; i<30; i++) a[i]=i; cout << a[3] << '\n'; delete[] a; cout << a[3] << '\n'; a[3]=9; cout << a[3]; cin >> pause; 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!
0
#2 Oct 7th, 2009
well one good practice to get into is to set your pointer to null after you delete it
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.
c++ Syntax (Toggle Plain Text)
int * a = new int[30]; delete [] a; a = NULL; //or a = 0;
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
![]() |
Similar Threads
- AfxBeginThread Memory Leak? (C++)
- quick C++ memory question (C++)
- Word Change Game (Posting Games)
- Temporary Internet files?? (Windows NT / 2000 / XP)
- Chess Program (C++)
- list template, release of memory (C++)
- C++ new / delete help (C++)
- How to "delete" this memory??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Could not deduce template argument...
- Next Thread: While loop Help
| Thread Tools | Search this Thread |
Tag cloud for delete, dynamic, memory
#include 3g add age amd apple array assign avatar bandwidth beginner black bluegene c++ chips cpu cron database ddr3 delete desktop development dos dropdown dynamic economy edit energy enterprise firefox flash formatdecimal gecko graphics gridview hard hardware ibm ibm.news ifstream include industry intel intelibm iphone java job leak linux load loops medicine memory microsd microsoft mozilla multi-core multidimensional nand news node nodes objects openoffice opensource pc pcm php processor progressbar ps3 python ram random recession recourse recursion redhat return russia search server speed ssd storage string sun supercomputer supercomputing table technology trends ubuntu upgrade url usb whileloop working x86 xml







