• Advertisement
Sign in to follow this  

Deleting value stored inside a pointer

This topic is 1908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So my situation is the following:

 

I have a pointer array that is initiated as NULL. The idea is that this array of pointers will hold instances of a class, which I create using the new operator:

 

myClass *ptr[10];
for(int x; x<=9; x++){ptr[x]=NULL;}

code...code...code...
ptr[5]=new myClass;
 

So far everything works fine, but now I need to know how to delete the instance inside the pointer without deleting the pointer / pointer array, itself.

 

Your help is appreciated biggrin.png

Share this post


Link to post
Share on other sites
Advertisement
You need to call delete for everything you call new for. You call new for ptr[x], for each x in the array, so you need to call delete for ptr[x] as well.
for(int x=0; x<10; ++x) {
    delete ptr[x];
}
Edited by Brother Bob

Share this post


Link to post
Share on other sites
delete ptr[5];

You can't (shouldn't) delete ptr because it wasn't newed:
myClass** arr = new myClass*[10];
for ( auto& obj : arr )
    obj = new myClass();

// ...

for ( auto& obj : arr )
    delete obj;
delete[] arr;

Share this post


Link to post
Share on other sites

You need to call delete for everything you call new for. You call new for ptr[x], for each x in the array, so you need to call delete for ptr[x] as well.

for(int x=0; x<10; ++x) {
    delete ptr[x];
}

I tried doing this, but it didn't do anything. The class instance contains a 3d object that is rendered when I run the program. Supposedly, when I delete the contents inside the  pointer (using delete ptr[x]) the image wont render when I run the program, but it does...

 

delete ptr[5];

You can't (shouldn't) delete ptr because it wasn't newed:

myClass** arr = new myClass*[10];
for ( auto& obj : arr )
    obj = new myClass();

// ...

for ( auto& obj : arr )
    delete obj;
delete[] arr;

I don't want to delete the pointer only what it's pointing to.

Share this post


Link to post
Share on other sites

If you delete the objects you have allocated, but then keep using the objects afterwards, your program is ill-formed. That does not mean that your program must crash or must produce an incorrect result. It may do that, but it doesn't have to. Nevertheless, your program is ill-formed.

Share this post


Link to post
Share on other sites
Deleting something doesn't automatically set the pointer to null. You'll need to nullify it yourself.
delete ptr[x];
ptr[x] = NULL;
But if you take this approach, then you need to check for NULL every time you access the array.

Share this post


Link to post
Share on other sites

Deleting something doesn't automatically set the pointer to null. You'll need to nullify it yourself.

delete ptr[x];
ptr[x] = NULL;
But if you take this approach, then you need to check for NULL every time you access the array.

If you need to litter your code with null-checks, then you have ownership issues that needs to be solved instead. You should not ask yourself if you need to check for null before accessing a pointer, you should ask yourself why the pointer can be deleted in the first place if you still need it.

Share this post


Link to post
Share on other sites




Deleting something doesn't automatically set the pointer to null. You'll need to nullify it yourself.

delete ptr[x];ptr[x] = NULL;
But if you take this approach, then you need to check for NULL every time you access the array.
If you need to litter your code with null-checks, then you have ownership issues that needs to be solved instead. You should not ask yourself if you need to check for null before accessing a pointer, you should ask yourself why the pointer can be deleted in the first place if you still need it.

If you delete the objects you have allocated, but then keep using the objects afterwards, your program is ill-formed. That does not mean that your program must crash or must produce an incorrect result. It may do that, but it doesn't have to. Nevertheless, your program is ill-formed.

I never said I was going to keep using the objects after I delete them. I said I was going to use the pointers and I don't want to delete the pointers, I want to delete what the pointers are pointing to.

Deleting something doesn't automatically set the pointer to null. You'll need to nullify it yourself.

delete ptr[x];ptr[x] = NULL;
But if you take this approach, then you need to check for NULL every time you access the array.
I was already planning for this though.
----------
Let me try and explain my idea.

I have two classes: class Monster and class Spawner.
The Monster class will have basic functions for movement, animations and keeping track of it's health, but it's the Spawner class that will control the creation and destruction of the Monster class instances.

So in the Spawner class I decided to create a pointer array of Monster class and also created two functions: createMonster() and killMonster().


//! definition of the createMonster() function...//! keep in mind that the pointer array has already been given the NULL value.
void createMonster()
{    
    while(int x; x<=9; x++)    
    {        
        if(ptr[x]==NULL)        
        {            
            ptr[x]= new Monster;        
        }    
    }
}


//! definition of the createMonster() function...
void killMonster(int x) //! the parameter is the particular pointer in the array that I want to clear.
{    
    code...code...code...    
    //! The line of code that will destroy the instance of the Monster class so that I can make the pointer NULL, in order to be re-    
    //! utilized at a later date to hold a new instance of the Monster class, goes here.    
    ptr[x]=NULL;
}
The code might look a bit empty, but I stripped it down to the bare bones in order to clarify the idea I'm trying to convey. Edited by Aluthreney

Share this post


Link to post
Share on other sites

Others have a handle on the specifics, but I saw some terminology mix-ups here that I wanted to ensure weren't holding you up as well. (I see them all the time with people learning C/C++.)

 

I have a pointer array that is initiated as NULL. The idea is that this array of pointers will hold instances of a class, which I create using the new operator...

So far everything works fine, but now I need to know how to delete the instance inside the pointer without deleting the pointer / pointer array, itself.

Your array of pointers won't "hold instances of a class". It will hold pointers: a list of addresses were instances of your class can be held. When you invoke the new operator, it instantiates whatever class you pointed it at and returns the address where that instance lives, which you land in your array of pointers there.
 

As others have explained, when you invoke the delete operator on an address, the delete operator will kill the instance of your class that lives there. This is where things can get tricky at times and is why you need to know the full picture here.

 

If you delete an instance but reference it later (because you still have the address in that pointer array) you'll get a seg fault; it's why it's a good idea to set a pointer to null after you delete it. This stuff can also be the source of memory leaks (if you lose that pointer before you delete the memory you allocated with new, that memory is never released and just hangs out, dead and wasting space.

 

I hope this is helpful. <3 Good luck.

Share this post


Link to post
Share on other sites





Deleting something doesn't automatically set the pointer to null. You'll need to nullify it yourself.

delete ptr[x];ptr[x] = NULL;
But if you take this approach, then you need to check for NULL every time you access the array.
If you need to litter your code with null-checks, then you have ownership issues that needs to be solved instead. You should not ask yourself if you need to check for null before accessing a pointer, you should ask yourself why the pointer can be deleted in the first place if you still need it.

If you delete the objects you have allocated, but then keep using the objects afterwards, your program is ill-formed. That does not mean that your program must crash or must produce an incorrect result. It may do that, but it doesn't have to. Nevertheless, your program is ill-formed.

I never said I was going to keep using the objects after I delete them. I said I was going to use the pointers and I don't want to delete the pointers, I want to delete what the pointers are pointing to.
The term "delete a pointer" means just that; you delete the object the pointer points to. That is, delete ptr does not delete ptr itself, it delete what ptr points to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement