Results 1 to 6 of 6

Thread: Pointers

  1. #1
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Pointers

    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.

    Qt Code:
    1. QList <QGraphicsItem*> items = this->graphScene->items();
    2. foreach(QGraphicsItem *i, items)
    3. {
    4. MyGraphItem *graphItem = qgraphicsitem_cast< MyGraphItem *>(i);
    5. // I did stuff with graphItem here now it’s time to delete it
    6. // not the object it points to though
    7. }
    To copy to clipboard, switch view to plain text mode 

    If I delete it like this;

    Qt Code:
    1. delete graphItem;
    To copy to clipboard, switch view to plain text mode 

    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;

    Qt Code:
    1. graphItem = NULL;
    2. delete graphItem;
    To copy to clipboard, switch view to plain text mode 

    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?
    Qt Code:
    1. MyGraphItem *GraphView::getItem(const QString &name)
    2. {
    3. QList <QGraphicsItem*> items = this->graphScene->items();
    4. if(items.isEmpty())
    5. return NULL;
    6.  
    7. foreach(QGraphicsItem *i, items)
    8. {
    9. // Here i am allocating memory
    10. MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
    11. if(typeid(*graphItem ) == typeid(MyGraphItem ))
    12. {
    13. // this is the object I want so return it
    14. if(graphItem ->name == name)
    15. {
    16. return graphItem ;
    17. }
    18. }
    19. // Hovewer if the object *graphItem is not wat I wanted
    20. // Should I delete it here, like so
    21. graphItem = NULL;
    22. delete graphItem;
    23. }
    24. return NULL;
    25. }
    To copy to clipboard, switch view to plain text mode 

    What about the returning a string below. I assume the receiving method should delete the pointer when it's no longer needed, right?
    Qt Code:
    1. QString *GraphAttribute::getItemName()
    2. {
    3. MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(this->parentItem());
    4. return &graphItem ->name;
    5. }
    To copy to clipboard, switch view to plain text mode 


    Another confirmation for returning pointer.

    Qt Code:
    1. GraphData *GraphItem::getAttribute(const QString &dataName)
    2. {
    3. // Here I have a QList that contains pointers of GraphData instances
    4. if(this->data.isEmpty())
    5. return NULL;
    6.  
    7. foreach(GraphData *d, this->data) // Getting them individually
    8. {
    9. if(d->data.name == dataName)
    10. return attr; // This is what I want, return it, this return pointer must be deleted
    11. // in caller method when it's no longer needed, right.
    12. else
    13. {
    14. // This is not what I want delete it
    15. d = NULL;
    16. delete d;
    17. }
    18. }
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 


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

    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Pointers

    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)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointers

    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

    Qt Code:
    1. MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. graphItem = NULL;
    2. delete grapItem;
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pointers

    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

  5. #5
    Join Date
    Jul 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointers

    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.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointers

    Came in here expecting a thread with nothing to do with qt programming. Leaving "satisfied".
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 3
    Last Post: 6th April 2012, 16:44
  2. Pointers...
    By salmanmanekia in forum General Programming
    Replies: 3
    Last Post: 8th June 2010, 02:48
  3. help with pointers and
    By sincnarf in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2007, 15:09
  4. Why should I use pointers ?
    By probine in forum Newbie
    Replies: 5
    Last Post: 9th December 2006, 21:16
  5. pointers gone bad
    By illuzioner in forum General Programming
    Replies: 2
    Last Post: 22nd April 2006, 21:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.