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
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

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

    Quote Originally Posted by high_flyer View Post
    Unless you define the vector as QVector<MyObject&> - then it indeed will take references.
    I'm pretty sure you can't do that, since references in C++ have their "oddies" (like the reference is for all it's life bound to an object and it should be set at declaration)

    So the remaining solution is QVector<MyObject*>

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

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

    You are correct Zlatomir.
    I have never tried it before actually, because it never made any practical sense.
    I just assumed it is possible.
    Now I tried it, and indeed, the compiler complains.
    Thanks for the heads up!
    ==========================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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

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

    Quote Originally Posted by high_flyer View Post
    I am not sure even why this code compiles, since you are trying to assign c to t but you didn't define an '=' operator for your Node class.
    Qt Code:
    1. Node t = n.GetChild(n.GetCount() - 1); //I don't know why this compiles, it should not
    To copy to clipboard, switch view to plain text mode 
    This is equivalent to:
    Qt Code:
    1. Node t(n.GetChild(n.GetCount()-1));
    To copy to clipboard, switch view to plain text mode 
    The copy constructor kicks in here. Besides if you don't declare your own default constructor, copy constructor or assignment operator, the compiler will provide them for you.


    Quote Originally Posted by ct View Post
    Well it does take reference.

    Qt Code:
    1. void push_back ( const T& x );
    To copy to clipboard, switch view to plain text mode 
    No, it takes a const reference. That's two totally different things.
    Last edited by wysota; 14th February 2011 at 19:01.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    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++

    Thanks, I got it. The copy constructor was being called all the time. I got the difference now.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

  5. #5
    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++

    One more thing on the topic though. Is it a good practice to avoid using pointers and use the stack space instead of heap ? That way there would be no memory leaks.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

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

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

    No; that will quickly lead to convoluted code in all but the simplest applications. Pointers are fundamental to C/C++; there is no reason to avoid them.

    If you're really worried about memory leaks, have a look at the STL auto_ptr class. Or run your code through a tool like valgrind to check for memory leaks, which is something you really ought to be doing anyway.

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

    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.

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

    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.

  9. #9
    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++

    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

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

    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.

  11. #11
    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++

    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

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

    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.

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.