Hi everyone,

I've been working with pointers for quite some time and I'm familiar with all the pointer-reference stuff. One of the things I don't get is the function of the delete keyword.

I have used this code to find out what the delete keyword does.

int main(int argc, char *argv[]) {
    int *i;
    if (i) cout << i << ": " << *i << endl;
    
    i = new int(6);
    if (i) cout << i << ": " << *i << endl;
    
    delete i;
    if (i) cout << i << ": " << *i << endl;
    
    i = new int(8);
    if (i) cout << i << ": " << *i << endl;
    
    delete i;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

This gave me this output:

[Address1]: -xxxxxxxx
[Address2]: 6
[Address2]: xxxxxxx
[Address2]: 8

So I'm confused with this output. Does the if (i) statement tell me that the pointer has been initialized, or does it always return true? How do I detect whether the address points to a valid location? (As in, no gigantic positive or negative numbers.) Why doesn't the address change when I call delete and call new again?

Help will be much appreciated.

Recommended Answers

All 5 Replies

right, there are some fundamental problems here:

int main(int argc, char *argv[]) {
    int *i; // pointer un-initialised - some random value in here

// because i points to a random memory address the de-referencing will most likely
// cause an unhandled exception on the print-out
// if you're lucky and the memory i points to belongs to your process then the below
// statement will print some garbage
    if (i) cout << i << ": " << *i << endl;
   
// now you're cooking with water: you create a new int on the heap and initialise
// it with the value 6 and assign its address on the heap to your pointer i 
    i = new int(6);
    if (i) cout << i << ": " << *i << endl;
    
// now you call the destructor on what i is pointing to which destructs the 
// integer you created above and frees the memory for re-use. it does *NOT*
// reset the value of i to NULL 
    delete i;

// because your i is not NULL the following print will be executed, but what i points
// to was destructed above and the memory freed so you are getting garbage for *i
    if (i) cout << i << ": " << *i << endl;
    
// good again
    i = new int(8);
    if (i) cout << i << ": " << *i << endl;

// final destruct - also good
    delete i;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for the advice, drkybelk.

I'm not saying this is the actual program, this is just the program I'm using to tell me what goes on within the computer when I use pointers and such.
I also know the basic good-to-know stuff, like initializing pointers and destructing them.

So to answer my own questions:
1) The if (i) statement will return false ONLY if i == NULL.
2) The address always points to a valid location, except if it is NULL. What's at that location is a different matter altogether. A usable location would be decided when calling 'new'.
3) The address doesn't change because I didn't set i to NULL.

Anything needs correcting?

1) -correct-
2) -incorrect-: if you do not initialise the pointer then it points to a *RANDOM* address, which means it might be valid or not, might also be NULL
3) -correct-

Right. Thanks!

If I can add something to this, here's a lecture by David J. Malan teaching computer science at Harvard explaining pointers and dynamic memory allocation, and here he explains heaps if you want to further understand what's going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.