Results 1 to 6 of 6

Thread: Why should I use pointers ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Why should I use pointers ?

    I can declare something like:

    QString string;

    I can also declare:

    QString * string;
    string = new QString();


    What is the difference, why should I use one instead of the other ?

  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: Why should I use pointers ?

    An object created using new operator (i.e. created "on the heap") will exist in memory until you destroy it with delete operator. To operate on such object, you need a pointer.

    The other kind of objects (i.e. created "on the stack", like this: QString string;), exist only within given scope. For example:
    Qt Code:
    1. int foo()
    2. {
    3. QString *ptr = new QString();
    4. for( ... ) {
    5. ...
    6. } // <-- "b" will be destroyed here
    7. delete ptr; // this destroys object pointed to by ptr, if you skip this line that object won't be destroyed at all
    8. return ...;
    9. } // <-- "a" will be destroyed here
    To copy to clipboard, switch view to plain text mode 
    Objects created on the stack will be destroyed for you, so you don't have to worry about memory leaks. This also means that if you want to use such object, you musn't let it go out of scope.

    In short: you use pointers only if you want to keep your objects in memory for longer or if you want to use polymorphism, while all other objects you create on the stack.

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why should I use pointers ?

    Very good explanation, now:

    I have looked at some books about Qt, the ones I know of show always something like:

    QTextBrowser *textBrowser;

    Why ?

    In the ".h" class couldn't I just do:

    QTextBrowser textBrowser;

    ????

  4. #4
    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: Why should I use pointers ?

    You could but this has two disadvantages:
    1. You have to include all header files needed by the class in every file that uses your class
    2. The order of destroying objects depends on the compiler. If it destroys objects in a wrong order, you'll get a memory error, because an area of memory will be freed twice. Consider this example:

    Qt Code:
    1. class A : public QWidget {
    2. public:
    3. A() : QWidget(){
    4. w1.setParent(&w2);
    5. }
    6. private:
    7. QWidget w1;
    8. QWidget w2;
    9. };
    To copy to clipboard, switch view to plain text mode 
    If compiler first frees w1 and then w2 then everything will be ok - w1 will be destroyed, so it will detach from its parent and then w2 will be destroyed.
    If the order is w2 and then w1, then when w2 is destroyed, it will destroy all its children including w1, so w1 will be destroyed as well. But the compiler doesn't know that w1 has already been destroyed, so will proceed normally and will try to delete w1, which will result in a double-free and a memory error.

  5. #5
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why should I use pointers ?

    So... what is the best approach:

    QTextBrowser textBrowser;

    or

    QTextBrowser *textBrowser

    ???

  6. #6
    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: Why should I use pointers ?

    It depends on the situation. It is safe to use pointers in every situation, but sometimes it is not required to do so (for example for widgets that don't have a parent or that will be destroyed earlier than their parents). There is really no "better". Qt handles most memory management for you, so you even don't have to remember about releasing the memory yourself, hence using "new" is not a big effort and can save your butt when writing for platforms that have a limited stack space

Similar Threads

  1. Is it possible to store pointers in a database ?
    By probine in forum General Programming
    Replies: 8
    Last Post: 5th April 2006, 21:28
  2. another Q_OBJECT and pointers (?)
    By caminoix in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2006, 21:06
  3. Pointers and Q_PROPERTY
    By andersin in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 14:05
  4. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 15:16

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.