Deallocating Array

Nov 3, 2010 at 6:02pm
In the following code I try to deallocate the memory for the Array and it only sets the first integer in the array to 0 and leaves the rest as it is. Is this supposed to happen if anyone knows? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
//256


int main(){
    using namespace std;
    
    int iArraySize;
    cout << "How many integers should be stored?" << endl;
    cin  >> iArraySize;
    int* ipArray = new int[iArraySize];
    
    for (int i = 0; i < iArraySize; i++){
        cout << endl;
        cout << "Input an integer:";
        cin  >> ipArray[i];
    }
    
    for (int i = 0; i < iArraySize; i++){
        cout << ipArray[i] << endl;

    }
        delete [] ipArray;
        
    
    
    for (int i = 0; i < iArraySize; i++){
        cout << ipArray[i] << endl;
    }

    cin.ignore();
    return 0;
}




Last edited on Nov 3, 2010 at 6:02pm
Nov 3, 2010 at 6:22pm
Try to initialize the array size to 0 before you get user input. When I ran your code, after I deleted the array I just had the address of where the array was as output?
Last edited on Nov 3, 2010 at 6:24pm
Nov 3, 2010 at 6:24pm
Just to clarify, when you delete something, it does not zero the memory, or do anything special. All it does is tell the OS "I'm done with this", so when you access the memory after you have deleted it, you could get valid numbers, garbage data, or maybe even a segfault (however unlikely).
Nov 3, 2010 at 9:06pm
@firedraco,

Does you mean that delete[] only makes memory available to be over-written? Isn't that analogous to the way DOS deletes a file by only deleting its directory entry but not the file itself?

If so, then lines 31 and 23 of krahl's program should always give the same output, shouldn't it, because there is nothing between the delete command and the cout command to over-write the memory where ipArray is located?
Nov 3, 2010 at 9:08pm
Does you mean that delete[] only makes memory available to be over-written?


That is all it's required to do. It may do more.

Isn't that analogous to the way DOS deletes a file by only deleting its directory entry but not the file itself?


Yes. Same idea.

If so, then lines 31 and 23 of krahl's program should always give the same output, shouldn't it,


No. Accessing invalid memory has undefined results. You can't expect anything to happen reliably.
Nov 4, 2010 at 8:45pm
So is ipArray called a dangling pointer or something after I delete it? for the reasons firedraco stated about accessing the memory after I delete it.

So infact if I had ran a larger program which accesses the memory more then I could get awkward numbers in the last for loop rather than 0, 2, and 3 for the size 3 array with original entries 1,2 and 3.

Am I right with this?

to fix the dangling pointer could I just put it to point to 0 or NULL?
Last edited on Nov 4, 2010 at 8:48pm
Topic archived. No new replies allowed.