Results 1 to 9 of 9

Thread: basic inherited or vector problem?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: basic inherited or vector problem?

    Quote Originally Posted by Methedrine View Post
    when the vector is resized it is very likely that it will change it's location inside memory and thus invalidate your pointer.
    Not quite, only the location of internal buffer will change. The object itself will remain where it was.

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

    Default Re: basic inherited or vector problem?

    The problem here was that the poster was declaring a pointer to the object, but no object was created, therefore he was accessing random values in memory which didn't form a std::vector object which led to a crash. One has to differenciate between a pointer to an object and address of an object. Their values have the same type (object*) but creating a pointer doesn't create an object. I can have several pointers pointing to the same object (and that's the whole point of using pointers).

    Qt Code:
    1. int var;
    2. int *ptr1 = &var;
    3. int *ptr2 = &var;
    4. int *ptr3 = ptr2;
    To copy to clipboard, switch view to plain text mode 
    Now if I modify var ( var = 7; ) all pointers will still point to the same object, so dereferrencing them will still give me "7". Moreover if I now do:
    Qt Code:
    1. int var2 = 5;
    2. ptr2 = &var2;
    To copy to clipboard, switch view to plain text mode 
    ptr2 will point to var2, but ptr3 will still point to var. The bottom line is that pointers are not tied to any objects, they just point to a place in memory, regardless of its contents.

Similar Threads

  1. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14

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.