Results 1 to 6 of 6

Thread: (SOLVED) Members of a class should be allocated on heap or stack?

  1. #1
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default (SOLVED) Members of a class should be allocated on heap or stack?

    I didn't find a good answer on the net...

    Should i allocate members of a class on heap or stack?

    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. MyClass();
    5.  
    6. private:
    7. QString *heap;
    8. QString stack;
    9. int i, j, k;
    10. AnotherClass anotherclass; // or *anotherclass?
    11. }
    To copy to clipboard, switch view to plain text mode 

    When should i use heap allocation?
    Last edited by giowck; 28th November 2009 at 22:43.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Members of a class should be allocated on heap or stack?

    Quote Originally Posted by giowck View Post
    Should i allocate members of a class on heap or stack?
    It depends... But normal is to create basic types at the stack. Is is also true for implicitly shared Qt classes.

    All other classes you should normally create on the heap. This is also true for classes which inherits QObject.

  3. #3
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Members of a class should be allocated on heap or stack?

    Quote Originally Posted by Lykurg View Post
    It depends... But normal is to create basic types at the stack. Is is also true for implicitly shared Qt classes.

    All other classes you should normally create on the heap. This is also true for classes which inherits QObject.
    So, for example: int, short, double... on the stack
    QWidget, QMenu... on the heap

    Right?

    Since, quint64 is a typedef int long long, i should put it on the stack, right?

    And where to allocate QStrings, QStriglists...? They are kids of QObject...

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Members of a class should be allocated on heap or stack?

    Quote Originally Posted by giowck View Post
    So, for example: int, short, double... on the stack
    QWidget, QMenu... on the heap

    Right?

    Since, quint64 is a typedef int long long, i should put it on the stack, right?
    Right, but for example if you want only show a dialog and destroy it right afterwards you would normally create it on the stack.
    Qt Code:
    1. function()
    2. {
    3. MyDialog d;
    4. d.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 
    But that's an exeption.

    And where to allocate QStrings, QStriglists...? They are kids of QObject...
    On the stack, because are not childs of QObject...

  5. The following user says thank you to Lykurg for this useful post:

    giowck (28th November 2009)

  6. #5
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: Members of a class should be allocated on heap or stack?

    yeah, now i get it! They aren't QObjects!! Thanks for your answer

    Cause i saw QString allocated on the stack in lastfm src code...

    But now i checked it...

    BTW thanks for the exception (function() on stack)

    Solved!

  7. #6
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: (SOLVED) Members of a class should be allocated on heap or stack?

    Quote Originally Posted by giowck View Post
    Qt Code:
    1. class MyClass {
    2. QString *heap;
    3. QString stack;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Just a note:
    MyClass * myClass = new MyClass;

    myClass.stack won't be on the stack but on the heap just like the rest of the class. Defining class members as values makes them "embeded" to the object memory.

  8. The following user says thank you to agnus for this useful post:

    giowck (29th November 2009)

Similar Threads

  1. Question about class members
    By serenti in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2009, 14:20
  2. 2 questions: QAuthenticator and stack / heap
    By Tito Serenti in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2009, 06:56
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. stack, heap and C#
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th August 2007, 18:40
  5. Replies: 4
    Last Post: 26th June 2007, 19:19

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.