Results 1 to 6 of 6

Thread: Basic question about allocation

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Basic question about allocation

    Suppose I have a function in the code of which I have
    Qt Code:
    1. QVector<int> myvector;
    To copy to clipboard, switch view to plain text mode 
    further down I append elements to myvector
    like this
    Qt Code:
    1. myvector.append(3)
    To copy to clipboard, switch view to plain text mode 
    Where does the allocation take place? On the stack? On the heap?
    What do I need to know about freeing the memory?
    Last edited by feraudyh; 23rd June 2010 at 18:15. Reason: layout

  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: Basic question about allocation

    The QVector object is on the stack (where you allocated memory for it) but he <most likely> allocates memory on the heap for the elements (and take care to release it), if you need to know more you can open the source code and see exactly how this works.

    The QVector will take care of the memory, you don't need to worry, it will not leak.

  3. #3
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question about allocation

    Quote Originally Posted by feraudyh View Post
    Suppose I have a function in the code of which I have
    Qt Code:
    1. QVector<int> myvector;
    To copy to clipboard, switch view to plain text mode 
    further down I append elements to myvector
    like this
    Qt Code:
    1. myvector.append(3)
    To copy to clipboard, switch view to plain text mode 
    Where does the allocation take place? On the stack? On the heap?
    What do I need to know about freeing the memory?
    I believe that allocation for both will take place on the heap. For variables where you do not specifically allocate memory with
    the "new" operator, temporary space is allocated for them on the stack. This means that all allocation and deletion will be handled
    for you, but it also limits the scope of those variables to that function on the stack where they are defined. Hence, every time you
    jump to a different part of the stack (usually through a different function call) if you can only send through values as parameters, not
    variables that can be modified. This method of scope and how memory allocation works is perhaps the most important thing to understand
    about c, and one of the more important things to understand in c++.

  4. #4
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question about allocation

    Just for clarification: when I said "both" i mean the "QVector" and the "int" with a value of 3 that you place into the vector.

  5. #5
    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: Basic question about allocation

    @SneakyPeterson: The QVector object is allocated on the stack and it has some pointers to typename T, witch (the pointer itself) it's also on stack, but it is used to allocate memory on the heap.

    If the vector itself will be on the heap, how it will be auto-deleted? It can't, it doesn't have the "parenting thing" (only the GUI parts on Qt have that).

  6. #6
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question about allocation

    I wasn't aware that QVector worked that way with primitives... Ah well.


Similar Threads

  1. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 07:46
  2. Basic question
    By giacomelli.fabio in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 00:12
  3. Basic QtScript question
    By jimboqt in forum Newbie
    Replies: 0
    Last Post: 23rd September 2008, 14:09
  4. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.