Results 1 to 9 of 9

Thread: display pointer memory address

  1. #1
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default display pointer memory address

    First post from a qt newbie.
    How can I achieve this in qt4. What header
    files would I need to include? If possible a simple code example would be really appreciated.

    int variable = 2;
    int* pointer = &variable;

    cout<<*pointer<<endl; //displays 2
    cout<<pointer<<endl; //displays the location of "variable"

    Thanks in advance.
    Milo
    Last edited by milosav; 4th June 2009 at 00:47. Reason: spelling error

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display pointer memory address

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. QApplication app(argc,argv);
    7.  
    8. QLabel label;
    9.  
    10. int variable = 2;
    11. int* pointer = &variable;
    12.  
    13. QString num=QString::number(variable);
    14. QString p=QString::number(pointer,16);
    15.  
    16. label.setText("number="+num+"pointer="+p);
    17. label.show();
    18.  
    19. return app.exec();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

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

    sparticus_37 (3rd September 2011)

  4. #3
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: display pointer memory address

    Mr Death,
    Thank you for the fast reply.
    Unfortunately the build finishes with the following error.

    error: call of overloaded ‘number(int*&, int)’ is ambiguous

    Build complains that QString does not seem to have a candidate number() function

    This is the same issue that led me to posting. I was unable to find a Qt function to solve the issue and figured I must be missing something simple.

    Any ideas ??

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: display pointer memory address

    I guess its getting confused in the pointer address
    try it with double conversion -
    QString p=QString::number((double)pointer,16);

  6. #5
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: display pointer memory address

    Same casting issue it would seem.

    Build Error
    error: invalid cast from type ‘int*’ to type ‘double’

    This should be really straight forward right?

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: display pointer memory address

    cast it to int:
    Qt Code:
    1. String p=QString::number((int)pointer,16);
    To copy to clipboard, switch view to plain text mode 
    this works for me;
    Last edited by faldzip; 4th June 2009 at 08:19. Reason: was [QTCLASS] instead of [CODE] :/
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. The following user says thank you to faldzip for this useful post:

    milosav (4th June 2009)

  9. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: display pointer memory address

    i have tested it... it works for int instead of double
    QString p=QString::number((int)pointer,16);

    edit: i was a lil late in reply... did not saw the above post..
    XX
    Last edited by nish; 4th June 2009 at 07:25. Reason: double post

  10. The following user says thank you to nish for this useful post:

    milosav (4th June 2009)

  11. #8
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: display pointer memory address

    Thanks a heap for your replies and your solution faldżip of casting to (int).

  12. #9
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: display pointer memory address

    Still useful after all these years. Thanks.

Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. How to display My Current System IP Address...???
    By maveric in forum Qt Programming
    Replies: 8
    Last Post: 4th August 2008, 17:22
  3. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.