Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi! I have same problem with delete poiter.
This's my Code.
C#
int main()
{
    vector<int*> list;
    int *a;
    for(int i=0;i<10;i++)
    {
        a=new int();
        *a=i;
        list.push_back(a);
    }
    int *value=new int();
    *value=4;
    list.push_back(value);
    cout<<"value : "<<*value<<endl;
    /*Delete pointer less 5*/
    for(int i=0;i<list.size();i++)
    {
        if(*list[i]<5)
        {
            delete list[i];
            list[i]=NULL;
        }
    }
    if(value!=NULL)
        cout<<"value : "<<*value<<endl;
    else
        cout<<"It's NUll"<<endl;
}


I think value must NULL but It's Not Null, so how do it NULL when it's deleted!
Posted

Between these lines:

C#
int *value=new int();
*value=4;
list.push_back(value);
cout<<"value : "<<*value<<endl;


and this line:

if(value!=NULL)


You do absolutely nothing to the variable named value. By what magic do you think it will get set to NULL?
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 16:05pm    
5+
// so how do it NULL when it's deleted ?

Please try the small modification :) :
C++
vector<int*&> list;
 
Share this answer
 
Comments
RaviRanjanKr 23-Dec-11 16:06pm    
5+
All the other solutions are valid, but it's also important to note that when you call delete on an allocated variable, the pointer will NOT automatically turn into a NULL value. The pointer itself will keep the value even if the variable is deallocated. If you want the pointer to become NULL, you have to manually set it.

int *value = new int; //allocate memory on the heap
delete value;         //value STILL contains a pointer, but it's no longer valid
value = NULL;         //Now the pointer has been reset
 
Share this answer
 
Comments
Chuck O'Toole 23-Dec-11 8:12am    
Albert, he is doing that for the element in the list:
delete list[i];
list[i]=NULL;
But he's expecting something to reach up into the other place that points to the memory, value and zap it too. That ain't happening.
Hi,

It is a good practice to assign NULL to a pointer after deleting it. Make sure that you are using the pointer in the following way always.


if(NULL != ptrVariable)
{
  //proccessing code
}


Thanks
 
Share this answer
 
v5
OK, let's take a little time to explore what is really the issue here.

First off, as others have pointed out, it is good practice to set a pointer variable, in your case value to NULL when you "free up" some allocated memory, using either free() or delete as appropriate. In other languages (like C#) the variable would be more properly called the "referencer" but in C/C++ it is the "pointer" variable.

However, as others have pointed out, you have really created a second copy of the "pointer" when you added value to the vector, so now you have two variables, each pointing to the same memory location. There is no magic on this planet that will run around and look at all the variables you have and set them to NULL if they point to some block of memory being "freed up". You have to do that yourself.

Now you do, in fact, follow this good practice when you use delete on the list[i] element and immediately set the pointer to NULL. So you are on the right track.

This brings up the issue of "ownership". Usually, having multiple pointers to the same allocated memory block is a bad idea. Oh, I can see times when that's useful but it has confused you in this usage. Once you added the value of value to the vector, you "gave up ownership" of that allocated memory to the vector. A fact that you recognize because you are doing the actual "freeing up" of that memory by referencing it through list.

What is another good practice is to also NULL out a pointer when you yield "ownership" to another variable / place. In this instance, if you really did not want a lingering pointer to the memory left in value, you should set it to NULL after adding it to the vector. In other words, once you added it to vector, value no longer "owns" what happens to that memory and at that point, to avoid future problems, you should have set the contents to NULL.
C#
list.push_back(value);
value = NULL;


This "giving up of ownership" is demonstrated quite nicely in the earlier part of your code where you use a single pointer, a inside a loop to allocate 10 different memory blocks. Clearly each time through the loop you overwrite (some would say "clobber") a with the next allocation but, fortunately for you, you created another copy of the pointer by placing it in the vector.
 
Share this answer
 
v2
This is exactly what you should expect.

Very simply: you are deleting object, not modifying a pointer to it, and this pointer itself is a different object; you don't modify it in any way. Deletion of an object is just informing the heap system that the object is no longer needed, so it's memory can be used later when required by some other allocation.

To make this more clear, consider you instantiate an object and assign new value to the pointer to it:
C++
int * value1 = new int();
int * value2 = new int();
value1 = value2; // will it do anything to an object created in first line? No!
value2 = 0; // will it do anything to an object created in first line? No!
// sorry, the access to the object created in first line is lost; you cannot even delete it


Are you getting the picture?

—SA
 
Share this answer
 
v2
Comments
trieuquangphuc_cf 22-Dec-11 21:26pm    
Thanks! but that it is not really what I want. There are cases in which we can not use the original pointer that must be referenced by a pointer to another and I really need to delete it to free up memory because there is not only an object when that object is much .
Sergey Alexandrovich Kryukov 23-Dec-11 11:14am    
No! This is what you really wanted -- understanding. And you still don't have is, according to this comment. There is nothing you should do about it. You only asked "why" and got a final answer.

There are two objects: a pointer which is stored on stack or elsewhere and object it points to, both accessed via the name of the pointer; as the pointed object does not have a permanent name. When you delete, you really delete, but the pointer still points somewhere. To some place in memory you should not use, dereference it, as this is not a valid address anymore.

Did you get it?

Anyway, please accept this correct answer formally (green button) -- thanks.
--SA
The problem is simple:

C++
int *value=new int();
*value=4;
list.push_back(value);


Makes a copy of pointer, so, it doesn´t get updated in the for loop.

SOLUTION (simple and easy):

C#
int *value=new int();
*value=4;
list.push_back(value);
//Get a pointer to the item in the vector
int** pvalue = &(list[10]);
...
...
...
if(*pvalue!=NULL)


Remember that when you get a pointer to an item in an vector you should not add more items because of reallocation unless you called reserve (reserve space) first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900