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
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    In addition to what has been said, the stack size is usually quite limitted, so any realy world application might soon run out of memory if all you use is the stack.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  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: Weird thing while trying to avoid pointers in C++

    Just a little addition to what has been said already: don't use auto_ptr it has some usage restriction (like you can't have an container of auto_ptr) and if i remember correctly it is on removal list of the new standard, so you will have to re-write code when you will upgrade to a C++0x compiler.

    Instead you can use boost shared_ptr (it should be included in tr1:: namespace it will be in std:: in C++0x) or Qt has QSharedPointer

    But anyway you need to understand pointers and memory management, even if Qt simplifies things (see parent-child relationship) you still need to know what is deleted by parent and what you should delete yourself.

  3. #3
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    In addition to what has been said, the stack size is usually quite limitted, so any realy world application might soon run out of memory if all you use is the stack.
    Is there a way to find out how much memory is the stack limited to. I'm just writing a prototype and yes memory is a issue. But I would only need around 200-300 mb max. I'm trying to import large text files.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    On Linux, you can say 'ulimit -a'; one of the numbers reported is stack size. On my machine, it is currently set at ~8MB, which is fairly typical.

  5. #5
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    Which space does STL containers like vector or hash_map use ?
    For example:

    Qt Code:
    1. int main(int argc,char **argv){
    2. vector<string> v;
    3. string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    4. //sizeof(s) = 8
    5. for(unsigned long i = 0;i< 18446744073709551615; i++)
    6. v.push_back(s);
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Isn't the vector being created on the stack ? Why doesn't this program crash even when the size goes way past stack limit (~8 MB) .
    Last edited by ct; 16th February 2011 at 16:26.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    Isn't the vector being created on the stack ?
    The vector object yes, the elements no.
    Think about it: since the elements are being dynamically added, how could they be added on the stack?
    That would mean that the vector has a predefined size.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Feb 2006
    Posts
    91
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    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

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

    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 08: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, 01:55
  2. doing a thing after the form is displayed
    By franco.amato in forum Newbie
    Replies: 3
    Last Post: 26th January 2011, 05:05
  3. Very strange thing
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 21st October 2008, 22:44
  4. There is QTimeEdit another thing in Qt4
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2006, 16: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.