PDA

View Full Version : simple pointer question



mickey
6th June 2006, 12:48
Hi, anyone could me explain why the first get a runtime error and second is ok? thanks


QString s;
QString* textLabel;
//*textLabel = s; //runtime error
textLabel = &s; //OK

jacek
6th June 2006, 13:09
Because textLabel wasn't initialized and pointed to a random part of memory.


QString a( "aaa" );
QString b( "bbb" );
QString* ptr = &a; // initialize the pointer

*ptr = b; // a == "bbb"
ptr = &b; // ptr points to b, so everything you will do with *ptr will change b
ptr->append( "X" ); // b == "bbbX" (and a is still "bbb")

sunil.thaha
6th June 2006, 13:13
QString *txtLabel;
*txtLabel = s; // WILL cause RT Err B'coz txtLAbel is not allocated any Memory :eek:


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



QString *txtLabel;
txtLabel = &s; // is fine because txtLabel now points to s

michael
6th June 2006, 16:17
Hi, anyone could me explain why the first get a runtime error and second is ok? thanks


QString s;
QString* textLabel;
//*textLabel = s; //runtime error
textLabel = &s; //OK


Or in addition to what everyone else has said you could do:



QString s;
QString* textLabel = new QString(s);

or


QString s;
QString* textLabel = new QString();
textLabel = &s; // as others have shown


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

mickey
6th June 2006, 17:13
Or in addition to what everyone else has said you could do:


QString s;
QString* textLabel = new QString(s);


If I undesrtand: in this example texlabel point to a portion of memory empty....?

michael
6th June 2006, 17:32
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


QString s = "Some String";
QString *stringPointer;
stringPointer = new QString(s);
....
delete stringPointer;


"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

iGoo
16th June 2006, 10:19
会导致内存访问异常?