Results 1 to 19 of 19

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

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

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

    I had this weird thing while I was trying to avoid pointers and just create objects on the stack.

    Qt Code:
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <vector>
    5. #include <cstdio>
    6. using namespace std;
    7.  
    8. class Node {
    9. public:
    10. string move;
    11. int state[10];
    12. Node(const Node &p) {
    13. move = "";
    14. memcpy(state,p.state,sizeof(int)*10);
    15. }
    16. Node(){
    17. move = "";
    18. for(int i = 0;i<10;i++){
    19. state[i] = i;
    20. }
    21. }
    22. void AddNode(Node p){
    23. this->childNode.push_back(p);
    24. }
    25. Node GetChild(int i) {
    26. return childNode[i];
    27. }
    28. int GetCount() {
    29. return this->childNode.size();
    30. }
    31. protected:
    32. vector<Node> childNode;
    33. };
    34.  
    35. class Move {
    36. public:
    37. Move() {
    38.  
    39. }
    40. void run() {
    41. Node n;
    42. n.move = "A100";
    43. operate(n);
    44. }
    45.  
    46. void operate(Node &n) {
    47.  
    48. Node c(n);
    49. c.move = "B100";
    50. n.AddNode(c);
    51. cout << "Output : " << endl;
    52. Node t = n.GetChild(n.GetCount() - 1);
    53. //Node t = n.childNode[0];
    54. for(int i = 0;i<10;i++)
    55. cout << t.state[i];
    56.  
    57.  
    58. cout << endl << t.move; // doesn't display B100 ????
    59.  
    60. }
    61.  
    62. };
    63.  
    64. int main(int argc,char** argv) {
    65.  
    66. Move m;
    67. m.run();
    68. return 0;
    69. }
    To copy to clipboard, switch view to plain text mode 

    Here is a sample of something that I'm trying to do. My question is why isn't B100 move displayed ? Do I need an assignment operator in the Node class ? I believed push_back takes references and that the node with move value B100 should be stored in the vector. Correct state array is stored but the move string is not, any suggestions ?
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

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

    ... where to begin...
    believed push_back takes references and that the node with move value B100 should be stored in the vector.
    Programming is not a religion.
    You don't believe stuff, you read the docs!
    Since your definition of your vector was not with reference, it takes copies, not references.
    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 
    maybe a default assignment operator is called, but it knows nothing about your Node members, so it can't possibly copy inner members of your Node members such as 'move'.
    ==========================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
    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++

    Well it does take reference.

    Qt Code:
    1. void push_back ( const T& x );
    To copy to clipboard, switch view to plain text mode 

    However, even if it takes copy, shouldn't there be a value of move in the vector ?

    I added the assignment operator
    Qt Code:
    1. Node& operator=(const Node &r) {
    2.  
    3. if(&r != this) {
    4. memcpy(this->state,r.state,sizeof(int)*10);
    5. this->move = r.move;
    6.  
    7. }
    8.  
    9. return *this;
    10. }
    To copy to clipboard, switch view to plain text mode 

    But still There is no output.
    Humans make mistake because there is really NO patch for HUMAN STUPIDITY

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

    Take a look at your copy constructor:
    Qt Code:
    1. Node(const Node &p) {
    2. move = ""; //!< change this to p.move
    3. memcpy(state,p.state,sizeof(int)*10);
    4. }
    To copy to clipboard, switch view to plain text mode 
    When you take out an object from vector, it's copy is made and previously assigned string is cleared in your copy-constructor.

    ------------------
    ok, maybe i was not clear enough, i mean this line:
    Qt Code:
    1. Node t = n.GetChild(n.GetCount() - 1);
    To copy to clipboard, switch view to plain text mode 
    this is copy - construction, its not the same as:
    Qt Code:
    1. Node t;
    2. t = n.GetChild(n.GetCount() - 1);
    To copy to clipboard, switch view to plain text mode 
    Assignment operator is not called anywhere in your code ( add cout's to see it yourself ).
    Last edited by stampede; 14th February 2011 at 09:41. Reason: additional comment

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

    Well it does take reference.
    That is true, however think about it:
    If it really only took a reference, and left it as a reference, all the original objects you would assign to it would need to stay alive, which is not the case.
    So it takes a reference, but internally it creates a copy.
    Last edited by high_flyer; 14th February 2011 at 10:20. Reason: removed wrong information
    ==========================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.

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

    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*>

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

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    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

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

    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

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

    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.

  12. #12
    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.

  13. #13
    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.

  14. #14
    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

  15. #15
    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.

  16. #16
    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

  17. #17
    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.

  18. #18
    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

  19. #19
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.