Sign in to follow this  
Alpha_ProgDes

Pointer deletion problem

Recommended Posts

It seems I'm not deleting the pointers correctly.... Because I can still access the pointer array after I delete it. Can someone point out the problem, which is obviously staring me in the face?
    int **d_pointer;
    int *pointer;

    
    pointer = new int[16];
    d_pointer = &pointer;
    
    pointer[1] = 16;
    cout << d_pointer[0][1] << endl;
    
    
    delete [] (*d_pointer);
    if ((*d_pointer) == pointer)
        cout << "Pointer deleted successfully." << endl;
    
    delete [] pointer;
    cout << *(pointer + 1) << endl;

Share this post


Link to post
Share on other sites
Quote:
It seems I'm not deleting the pointers correctly....
Because I can still access the pointer array after I delete it.
Can someone point out the problem, which is obviously staring me in the face?


Well one thing to keep in mind is that just because you delete it, it does not mean the values are taken from memory. They could still be residing there regardless.

Anyways since you never allocate memory for d_pointer, just use:

delete [] pointer;


And that's it. If you still do not think it's being deleted I'll give it a quick run in a mmgr and see what it says.

- Drew

[edit]

Results:

When I used your code, my program crashed. When I took out the
 delete [] (*d_pointer); 
it worked great. It generated this:


----------------------------------------------------------------------------------------------------------------------------------
| Memory leak report for: 02/08/2005 23:28:13 |
----------------------------------------------------------------------------------------------------------------------------------


Congratulations! No memory leaks found!







Now when I commented out the line
delete [] pointer;
I got this:

----------------------------------------------------------------------------------------------------------------------------------
| Memory leak report for: 02/08/2005 23:26:33 |
----------------------------------------------------------------------------------------------------------------------------------


1 memory leak found:

Alloc. Addr Size Addr Size BreakOn BreakOn
Number Reported Reported Actual Actual Unused Method Dealloc Realloc Allocated by
------ ---------- ---------- ---------- ---------- ---------- -------- ------- ------- ---------------------------------------------------
000065 0x00AAE4C0 0x00000040 0x00AAE4B0 0x00000060 0x0000003C new[] N N ??(00000)::??





So just remove the
delete [] (*d_pointer);
and do not access the memory anymore and you are great. Set the 'pointer' variable to NULL after you deleted it as well. Also note that 'd_pointer' will be pointing to an illegal memory location so you might wanna use that in a different way or NULL it out as well.

- Drew

Share this post


Link to post
Share on other sites
Yes it is. I just choose the latter for simplicity's sake. But you cannot delete the same memory twice like how you were doing it or access it after it has been deleted. It is extremely unsafe to do so. It has to be either or for the deletion method, which is also why I reccomend NULL'ing it after the delete because a delete 0 turns into a no-op.

Share this post


Link to post
Share on other sites
As stated, delete [] *d_pointer; and delete [] pointer; are the same thing, the obvious clue that you're doing something wrong here is that you're allocating 1 chuck of memory, and deleting two.

int **d_pointer;
int *pointer;

// Stack:
// 1: d_pointer --> ??????
// 2: pointer --> ??????

pointer = new int[16];
d_pointer = &pointer;

// Stack:
// 1: d_pointer --> 2
// 2: pointer --> 3
// Heap:
// 3: data

pointer[1] = 16;
cout << d_pointer[0][1] << endl;

delete [] (*d_pointer);

// Stack:
// 1: d_pointer --> 2
// 2: pointer --> 3 (doesn't exist)

if ((*d_pointer) == pointer)
cout << "Pointer deleted successfully." << endl;

// This will still be true, delete doesn't change change the value of a pointer.
// d_pointer still points to pointer, and pointer still points to the old
// address of the data, even though there's nothing there.

delete [] pointer;
// The address pointer is pointing to isn't valid, it has already been deleted.

cout << *(pointer + 1) << endl;
// So you've deleted the pointer twice and you're still trying to use it. Oh, yeah, that's really smart.

Share this post


Link to post
Share on other sites
delete [] (*d_pointer);
if ((*d_pointer) == pointer)
cout << "Pointer deleted successfully." << endl;

error checking deallocation is rather pointless. What you are doing here, is comparing the memory locations where the pointers were set. delete doesnt change the pointers value. In other words, this would print it even if you remove the deallocation.

as mentioned, always null out pointers after deallocation.

delete [] (*d_pointer);
d_pointer = 0;
pointer = 0;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this