Hi, i just want to know about dynamioc memopry allocation.i allocated the space using new operator for an entire array of 5 numbers of type int. i deallocated the space using delete keyword for the entire array. now my doubt is hw we can see that the memory hasbeen deallocated.if we think that the memory is deallocated in side the system implictly trhen when again i started printing the array the elements inside it again gettng printed.so after the space hasbeen deallocated hw the elements inside gets printed.if this is possible then we can save many bytes of space. if the memory hasbeen deallocated then from wher the elements geting printed. iam postng u the program related to my doubt Code: #include <iostream> using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } for (n=0; n<i; n++) cout << p[n] << ", "; return 0; } this is the program done by me wher i got the above doubts
Hello , If u saw carefullly after deleting P array only junk values were printed not the ones which are assigned to it. so it means that space got freed. i Hope this shall clear ur doubt regards ~ranjit
To solve your problem you should visit in following link...... http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.11