Results 1 to 19 of 19

Thread: Weird thing while trying to avoid pointers in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Posts
    91
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: Weird thing while trying to avoid pointers in C++

    But we don't need to deallocate the memory of vector or other STL containers ? Does it mean that as soon as the object created on the stack runs out of scope, it's memory on heap is deallocated automatically ?
    If it does clean up, then I should be ok. I have designed the class such that instead of dynamically allocating objects, i'm inserting it in the vector so that when it runs out of scope, memory will be freed.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: Weird thing while trying to avoid pointers in C++

    But we don't need to deallocate the memory of vector or other STL containers ?
    If you use the container like this:
    Qt Code:
    1. std::vector<MyClass> v;
    2. v.push_back( MyClass() );
    3. //...
    To copy to clipboard, switch view to plain text mode 
    you don't need to de-allocate anything, vector will remove all of it's elements when it runs out of scope. But if you store pointers:
    Qt Code:
    1. std::vector<MyClass*> v;
    2. v.push_back( new MyClass() );
    3. //...
    To copy to clipboard, switch view to plain text mode 
    then you need to remove the objects yourself. When vector runs out of scope it still removes all of it's elements, but this time no ~MyClass destructor is called - since it's just a pointer that is removed - and if you don't do proper cleanup yourself, you'll have a memory leak.

    Does it mean that as soon as the object created on the stack runs out of scope, it's memory on heap is deallocated automatically ?
    Qt Code:
    1. class A{
    2. public:
    3. A(){
    4. b = new B();
    5. }
    6. ~A(){
    7. }
    8. B * b;
    9. };
    10.  
    11. //...
    12. {
    13. A a;
    14. } // a (created on stack) goes out of scope here...
    To copy to clipboard, switch view to plain text mode 
    ... but a.b (object created on heap) will not be removed automatically, you need to call "delete b" yourself.
    Last edited by stampede; 17th February 2011 at 09:48. Reason: to many spaces in code

Similar Threads

  1. KDChart. What do you thing about?
    By Nadia in forum Qt-based Software
    Replies: 1
    Last Post: 15th February 2011, 02:55
  2. doing a thing after the form is displayed
    By franco.amato in forum Newbie
    Replies: 3
    Last Post: 26th January 2011, 06:05
  3. Very strange thing
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2008, 23:44
  4. There is QTimeEdit another thing in Qt4
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2006, 17:06

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.