Results 1 to 3 of 3

Thread: very basic doubt

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default very basic doubt

    Hello all,
    Well i am starting Qt with a book wherein i read that to allow object Trees(object hierarchies,i.e. child objects automatically get deleted when parent is deleted) the objects must be on heap.Why is it so;if they were on stack,using the stack pointer they could be deleted in the same way.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: very basic doubt

    Objects on the stack are deleted when the scope ends, but the ones on the heap stay until the programmer calls delete on the pointer (that was used to allocate them on heap), or the QObject parent-child relationship delete the object on the heap or the worse case: remain as a memory leak...

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: very basic doubt

    Creating on the stack can give you more problems than it solves, you have less control on when the object is deleted and could cause great confusion in debugging. For example, if you use it as a child of a parent, then there is a chance that the object could be double-freed (once by going out of scope and again by a parent).

Similar Threads

  1. A simple basic fundamental doubt !
    By symsahoo in forum Qt Programming
    Replies: 4
    Last Post: 14th August 2008, 10:46
  2. doubt
    By deepa.selvaraj in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2007, 09:23
  3. Doubt ?
    By Cutey in forum Qt Tools
    Replies: 2
    Last Post: 3rd March 2007, 09:45
  4. Basic C++ doubt
    By munna in forum General Programming
    Replies: 8
    Last Post: 30th November 2006, 20:54

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.