Results 1 to 6 of 6

Thread: Pointer to Pointer

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Pointer to Pointer

    Hi,

    Qt Code:
    1. int* a = new int;
    2. int** b = &a;
    3. delete a;
    4. a = new int;
    To copy to clipboard, switch view to plain text mode 

    When creating the new "int" pointer "a", can "**b" be accessed or it will access to a bad memory data?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pointer to Pointer

    b should work fine!

    (address=XXX, pointsTo=XXX where XXX are memory addresses)

    Qt Code:
    1. int* a = new int;
    To copy to clipboard, switch view to plain text mode 
    a: address=1 pointsTo=10
    Qt Code:
    1. int** b = &a;
    To copy to clipboard, switch view to plain text mode 
    b: address=2 pointsTo=1 (-> which points to 10)

    Qt Code:
    1. delete a;
    To copy to clipboard, switch view to plain text mode 
    a: address=1 pointsTo=NULL
    (b: address=2 pointsTo=1 (-> which points to NULL))

    Qt Code:
    1. a = new int;
    To copy to clipboard, switch view to plain text mode 
    a: address=1 pointsTo=11
    b: address=2 pointsTo=1 (-> which points to 11)

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to Pointer

    Hi,

    Thanks,

    It is what I was expecting.
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pointer to Pointer

    Ups, found an error:

    Quote Originally Posted by Lykurg View Post
    Qt Code:
    1. delete a;
    To copy to clipboard, switch view to plain text mode 
    a: address=1 pointsTo=NULL
    (b: address=2 pointsTo=1 (-> which points to NULL))
    There "a" still points to "10". So in that short period you have dangling pointers: "a" and "b".

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pointer to Pointer

    Hi,

    Quote Originally Posted by Lykurg View Post
    Ups, found an error:



    There "a" still points to "10". So in that short period you have dangling pointers: "a" and "b".
    Yes but this is not a problem because I have QMutex to lock this kind of operations.

    Thanks,
    Òscar Llarch i Galán

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pointer to Pointer

    A pointer just (a the name says) points to a piece of memory.
    When you free that memory all accesses to it become invalid, both 'direct' ones (via the freed pointer) and also all other ways you manage to access the address of the freed memory.

    So in your example, you may neither access *a or **b.
    (The pointers themselves are valid, but the memory finally pointed to, is not.)

Similar Threads

  1. Dragging a pointer
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2009, 03:51
  2. Pointing Two Dimensional Pointer to return value
    By babu198649 in forum General Programming
    Replies: 5
    Last Post: 24th April 2009, 19:16
  3. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 16:51
  4. pointer in a class
    By mickey in forum General Programming
    Replies: 23
    Last Post: 26th May 2008, 15:52
  5. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04

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.