PDA

View Full Version : Pointers



devla
30th October 2012, 05:26
Hi everybody,

I will be asking some confirmations instead of questions simple because even as a python developer I do not have (any apparent at least) problem with the code examples below. I wanted to create this topic to have my understanding of pointers as concrete as possible. I think along with myself, many people can benefit it.

I will be given examples topic by topic, again, all codes below are compile(not sure leaks though). Here I go.

1. Pointers point
Yeah, we know that. However, if I delete a pointer which points to an object such as QGraphicsItem, it deletes that object.



QList <QGraphicsItem*> items = this->graphScene->items();
foreach(QGraphicsItem *i, items)
{
MyGraphItem *graphItem = qgraphicsitem_cast< MyGraphItem *>(i);
// I did stuff with graphItem here now it’s time to delete it
// not the object it points to though
}


If I delete it like this;


delete graphItem;

it deletes the graph item it points to. I just want to delete (remove from the memory) graphItem. Not the item it points to. So I am deleting it like this;


graphItem = NULL;
delete graphItem;

I was wondering whether this way is the right way to delete the pointer while not touching the object it points to.



2. Returning Pointers
Is the approach I am using below a right one?


MyGraphItem *GraphView::getItem(const QString &name)
{
QList <QGraphicsItem*> items = this->graphScene->items();
if(items.isEmpty())
return NULL;

foreach(QGraphicsItem *i, items)
{
// Here i am allocating memory
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
if(typeid(*graphItem ) == typeid(MyGraphItem ))
{
// this is the object I want so return it
if(graphItem ->name == name)
{
return graphItem ;
}
}
// Hovewer if the object *graphItem is not wat I wanted
// Should I delete it here, like so
graphItem = NULL;
delete graphItem;
}
return NULL;
}

What about the returning a string below. I assume the receiving method should delete the pointer when it's no longer needed, right?


QString *GraphAttribute::getItemName()
{
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(this->parentItem());
return &graphItem ->name;
}



Another confirmation for returning pointer.


GraphData *GraphItem::getAttribute(const QString &dataName)
{
// Here I have a QList that contains pointers of GraphData instances
if(this->data.isEmpty())
return NULL;

foreach(GraphData *d, this->data) // Getting them individually
{
if(d->data.name == dataName)
return attr; // This is what I want, return it, this return pointer must be deleted
// in caller method when it's no longer needed, right.
else
{
// This is not what I want delete it
d = NULL;
delete d;
}
}
return false;
}


Would you please let me know if you see any problem in my code?

Thanks

Santosh Reddy
30th October 2012, 06:17
I was wondering whether this way is the right way to delete the pointer while not touching the object it points to.
This is basic C++ nothing Qt, delete operator will delete the object pointed by the pointer. you cannot delete the pointer variable, as it is allocated on stack, it will be automatically deleted once block of code in which the pointer is declared goes out of scope


Would you please let me know if you see any problem in my code?
Return the pointer to what ever in which every way you want, but make sure the calling function will not delete it (unless it is the requirement). Because if calling function deletes the pointer (implied that object it points to) the supplier copy of the pointer will be pointing to an object which is deleted (eventually causing segmentation fault on most systems)

devla
30th October 2012, 07:07
yes it's C++ I was just giving the example with qt objects.

hmm, i am a bit confuse now. because i am allocating memory (creating a variable) that points to an object previously created by calling


MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);

and it is in the heap, right? so when I am done with graphItem I must delete it explicitly since it won't delete itself. In order to delete it I am using following code.



graphItem = NULL;
delete grapItem;

so does this delete (remove) the allocated memory for grapItem variable itself? beacuse when I do it, the object it points to does not get deleted. As far as I now *graphItem must be deleted by using delete keyword since it's in the heap.

Am I right on those things?

Same thing applies the returning pointer. Say in current method I obtained a pointer which created by the method I called. Say I am done with it and now I need to delete the created pointer which points a previously created object (as you say assuming that the pointer is no longer needed). Isn't this the right way to follow?

Thank you very much for your post.

Ginsengelf
30th October 2012, 09:06
No, you are wrong. Your first code example only tells the compiler to create a pointer to a MyGraphItem on the stack, and to treat i as a pointer to MyGraphItem. There is no allocation on the heap here. Calling delete in your second example will do nothing in the best case (may even crash, I'm not sure about deleting pointers to NULL).
If you omit the graphItem = NULL; line, delete will delete the object pointed to by graphItem but do nothing to the pointer itself (you get a dangling pointer that points to no usefull area).

Ginsengelf

devla
30th October 2012, 20:48
I see, *graphItem is not heap because I did not use new keyword?... so apparently this means that I do not need to delete it right? It will be deleted when execution goes out of scope

Thanks a lot for taking time to post replies.

amleto
30th October 2012, 21:56
Came in here expecting a thread with nothing to do with qt programming. Leaving "satisfied".