Deleting Pointers

I have a pointer array declared as such:
Object* m_objects[cMaximumNumberOfObjects];

I have a function with the below code in it:

Object* pSourceObject = NULL;
for(unsigned int i = 0; i < m_numberOfObjects; i++)
{
if(m_objects[i]->m_objectId == aObjectId)
{
pSourceObject = new CombatObject();
pSourceObject->m_objectId = m_objects[i]->m_objectId;
pSourceObject->m_bitFlags = m_objects[i]->m_bitFlags;
pSourceObject->SetPosition(m_objects[i]->GetPosition());

break;
}
}

So at this point I'd imagine the pointer structure be something like:
pSourceObject (points to)-> m_object[i] (points to)-> data

At the end of my function I have:
delete pSourceObject;

My question is, wouldn't calling DELETE here delete the stored data?
So at this point I'd imagine the pointer structure be something like:
pSourceObject (points to)-> m_object[i] (points to)-> data


Not quite.

When you use new, as the name implies, you are creating a new object. This object has no name or scope. For purposes of this example... let's call this object 'foo'.

When you delete, you are not deleting the pointer, you are deleting whatever the pointer points to. In this case, since pSourceObject points to 'foo', if you were to delete pSourceObject; you would be deleting foo, after which, pSourceObject would point to unallocated/bad memory.

In your code:
- each pointer in your m_objects array point to whatever they point to (you don't show what they point to in this code)
- pSourceObject points to the newly created 'foo' object

At no point in this code do you tie m_objects and pSourceObject together, other than making the two objects share the same information. Therefore to answer your question:

My question is, wouldn't calling DELETE here delete the stored data?


You have no shared data. m_objects[i] points to some object (let's call it 'bar'), and pSourceObject points to 'foo'. Foo and Bar are two completely independent and different objects -- nothing about them is shared.

If you were to delete pSourceObject, then 'foo' would be destroyed, but 'bar' would remain completely unchanged.


Whoops -- I misread your quesiton, I thought you said would it delete the "shared" data, not stored data. Sorry.

So then the answer is "yes". If you delete pSourceObject; then foo is destroyed which means all its associated data disappears.
Last edited on
So based on your example given:

pSourceObject (points to)-> foo (points to)-> data in memory(in my case an object)

If you delete pSourceObject; then foo is destroyed which means all its associated data disappears.


based on this statement i'd assume the pointers after the DELETE would be:

pSourceObject (points to)-> NOTHING
foo (points to)-> NOTHING
data in memory(in my case an object) = Garbage/Free

TBH, this doesnt make much sense. I would instead expect this to happen:

pSourceObject (points to)-> NOTHING
foo (points to)-> data in memory(in my case an object)

At least thats what I'd prefer it to do. Am I correct in thinking this way?
Last edited on
Have a look at the class destructor, because the destructor is called when you use delete.
based on this statement i'd assume the pointers after the DELETE would be:

pSourceObject (points to)-> NOTHING
foo (points to)-> NOTHING
data in memory(in my case an object) = Garbage/Free

TBH, this doesnt make much sense. I would instead expect this to happen:

pSourceObject (points to)-> NOTHING
foo (points to)-> data in memory(in my case an object)


'foo' seems to be confusing you. It's an object, not a pointer. And as such, it can't point to anything.

Maybe this diagram will help:

http://i.imgur.com/DPgdgM0.png


BlatantlyX wrote:
Have a look at the class destructor, because the destructor is called when you use delete.


While this is true, it doesn't really help visualize what is happening with allocated memory and object scope.
Last edited on
Thank you for the effort of the diagram.

However, I was only following your previous example of foo, and forgot to put in that foo is a pointer to some place in memory.

From OP, m_object[i] to be exact.

I have modified your image to suit what I am talking about here:
http://shawnfreeman.us/misc/pointer_explaination.png

So the question is, what happens to both pointers pSourceObject, foo, and the actually data in memory given my diagram?
forgot to put in that foo is a pointer to some place in memory.


No, see, you are still misunderstanding.

foo is not a pointer. It cannot point/refer to "some place in memory". It IS the place in memory. It's the object.

So your 3rd thing in your diagram with the object ID and stuff... that isn't another area in memory... that's foo. That is what you create when you use the new keyword -- you create all those variables. You create (or "allocate") that block of memory. You create that object.


EDIT:

If your goal is not to create a new object, but rather to use an existing object -- then you do not want to use new. You want to make pSourceObject point to the object in question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Object* pSourceObject = NULL; 
for(unsigned int i = 0; i < m_numberOfObjects; i++)
{
    if(m_objects[i]->m_objectId == aObjectId)
    {
        // if you don't want a new object, then don't use new to create one
        /*pSourceObject = new CombatObject();
        pSourceObject->m_objectId = m_objects[i]->m_objectId;
        pSourceObject->m_bitFlags = m_objects[i]->m_bitFlags;
        pSourceObject->SetPosition(m_objects[i]->GetPosition());  */

        // instead, just point to the object you want:
        pSourceObject = &m_objects[i];
        break; 
    }
}


Now, foo no longer exists because we don't use new. Which also means we don't have to delete anything.

Instead, pSourceObject points to an existing object, 'm_objects[i]'
Last edited on
Topic archived. No new replies allowed.