Results 1 to 7 of 7

Thread: simple pointer question

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default simple pointer question

    Hi, anyone could me explain why the first get a runtime error and second is ok? thanks
    Qt Code:
    1. QString* textLabel;
    2. //*textLabel = s; //runtime error
    3. textLabel = &s; //OK
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple pointer question

    Because textLabel wasn't initialized and pointed to a random part of memory.

    Qt Code:
    1. QString a( "aaa" );
    2. QString b( "bbb" );
    3. QString* ptr = &a; // initialize the pointer
    4.  
    5. *ptr = b; // a == "bbb"
    6. ptr = &b; // ptr points to b, so everything you will do with *ptr will change b
    7. ptr->append( "X" ); // b == "bbbX" (and a is still "bbb")
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    mickey (6th June 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple pointer question

    Qt Code:
    1. QString *txtLabel;
    2. *txtLabel = s; // WILL cause RT Err B'coz txtLAbel is not allocated any Memory :eek:
    To copy to clipboard, switch view to plain text mode 

    It's a basic question of c++. I sugguest that u refresh your pointer concepts

    Qt Code:
    1. QString *txtLabel;
    2. txtLabel = &s; // is fine because txtLabel now points to s
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  5. The following user says thank you to sunil.thaha for this useful post:

    mickey (6th June 2006)

  6. #4
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple pointer question

    Quote Originally Posted by mickey
    Hi, anyone could me explain why the first get a runtime error and second is ok? thanks
    Qt Code:
    1. QString* textLabel;
    2. //*textLabel = s; //runtime error
    3. textLabel = &s; //OK
    To copy to clipboard, switch view to plain text mode 
    Or in addition to what everyone else has said you could do:

    Qt Code:
    1. QString* textLabel = new QString(s);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QString* textLabel = new QString();
    2. textLabel = &s; // as others have shown
    To copy to clipboard, switch view to plain text mode 

    Basic difference:
    In the first example I give textLabel is allocated its own memory and the pointer points to that location. So changing textLabel would have no effect on QString s.

    In the second example the pointer textLabel point to the memory location assigned to QString s so changes made to textLabel would show up in s as they are actually the same variable (as far as memory is concerned).

    Michael
    Last edited by michael; 6th June 2006 at 15:19.

  7. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: simple pointer question

    Quote Originally Posted by michael
    Or in addition to what everyone else has said you could do:
    Qt Code:
    1. QString* textLabel = new QString(s);
    To copy to clipboard, switch view to plain text mode 
    If I undesrtand: in this example texlabel point to a portion of memory empty....?
    Regards

  8. #6
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple pointer question

    Quote Originally Posted by mickey
    If I undesrtand: in this example texlabel point to a portion of memory empty....?
    C++ provides the "new" and "delete" operaters to allocate and deallocate memeroy in your program as needed.

    The above code could be rewritten for easier explaination as
    Qt Code:
    1. QString s = "Some String";
    2. QString *stringPointer;
    3. stringPointer = new QString(s);
    4. ....
    5. delete stringPointer;
    To copy to clipboard, switch view to plain text mode 

    "QString s = "Some String";
    As you probably know creates a new QString object named "s".

    QString* stringPointer;
    Tells the computer you are creating a new pointer that will point to an object in memory of type QString. However, at this point the pointer does not point to any memory location, or more correctly 0 or a random one. You could take this pointer and point it to the memory location of "s" with "stringPointer = &s" or you can allocate more memory to hold the object for this pointer using the new operator.

    stringPointer = new QString(s);
    This tells the computer to allocate a section of memory the size of a QString() and set the value of stringPointer to its memory location. Putting the "s" in QString() as "QString(s)" as defined in the QString constructor function QString::QString(QString string) also tells the computer to make sure that the new QString object created in memory should have the same text as the QString "s" you created earlier. You could have also created an empty QString pointer by calling "QString()" instead of "QString(s)".

    And lastly if you are using pointers you should remember to delete them so as to not have a memory leak in your program using delete. So when you are done with your pointer you should call "delete stringPointer" IF and only if you allocted more memory for the pointer using "new".

    I hope this helps you...

    EDIT:
    I thought I should add this about allocating and deallocating memory for QObjects etc. When you, for example, subclass a QObject class and create a pointer to newly allocated memory to a new QObject such as a QPushButton using "new" you do not need to call delete as Qt will keep up with it for you and delete the memory allocated for the pointer when the parent dies. See this tutorial for more information on that: http://doc.trolltech.com/4.1/tutorial-t4.html
    Last edited by michael; 6th June 2006 at 16:55.

  9. #7
    Join Date
    May 2006
    Posts
    32
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Smile Re: simple pointer question

    会导致内存访问异常?

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. Simple Question on getExistingDirectory
    By Naveen in forum Qt Programming
    Replies: 6
    Last Post: 3rd March 2006, 09:44
  3. a simple question: spinbox
    By mickey in forum Newbie
    Replies: 3
    Last Post: 27th February 2006, 15:37
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03

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.