qt显示特定地址的内存值

时间:2016-10-10 09:06:15

标签: c++ qt pointers

美好的一天

我熟悉指针和参考资料,

如果问题包含的信息超出了要求,那么

道歉,但这可能对其他人有帮助

目的:

我将test_string的内存地址作为&test_string传递给构造函数,构造函数定义使用QString *ptr_test作为参数接受内存地址,并将新类中的this赋值给a指针位置由QString *pointer = ptr_test定义。

这实际上是一个传递引用方法,但只是一直使用内存地址。

问题:

现在我有一个pointer的内存地址,我可以使用哪种方法在地址显示值和/或将其分配到例如QString pointerValue其中pointerValue将包含pointer

中指定的内存地址的值

有关信息:

致电代码

QString test_string = "This string is created in loginwindow and pointer sent to dialog";
qDebug() << "Displaying orignial variable";
qDebug() << "Displaying test_string = " << test_string;
qDebug() << "Displaying &test_string = " << &test_string;
qDebug() << "/nDisplaying tests:";
...
//code inbetween
...
LoginStatusDialog *dlgConnectStatus = new LoginStatusDialog(test_string, test_string, test_string, &test_string);
dlgConnectStatus->exec();

标题

public:
    LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent = 0);
    ~LoginStatusDialog();

private:

    Ui::LoginStatusDialog *ui;
    QString login, key, auth_tok, *pointer;

实施

LoginStatusDialog::LoginStatusDialog(const QString &_login, const QString _key, const QString _auth_tok, QString *ptr_test, QWidget *parent) :
    QDialog(parent), ui(new Ui::LoginStatusDialog), login(_login), key(_key), auth_tok(_auth_tok), pointer(ptr_test)
{
    qDebug() << "Passed with const &_login";
    qDebug() << "Displaying login = " << login;
    qDebug() << "Displaying _login = " << _login;
    qDebug() << "Displaying &login = " << &login;
    qDebug() << "Displaying &_login = " << &_login;

    qDebug() << "\nPassed with const _auth_tok";
    qDebug() << "Displaying auth_tok = " << auth_tok;
    qDebug() << "Displaying _auth_tok = " << _auth_tok;
    qDebug() << "Displaying &auth_tok = " << &auth_tok;
    qDebug() << "Displaying &_auth_tok = " << &_auth_tok;

    qDebug() << "\nPassed address with const _key";
    qDebug() << "Displaying key = " << key;
    qDebug() << "Displaying _key = " << _key;
    qDebug() << "Displaying &key = " << &key;
    qDebug() << "Displaying &_key = " << &_key;

    qDebug() << "\nPassed with *ptr_test";
    qDebug() << "Displaying test = " << pointer;
    qDebug() << "Displaying ptr_test = " << ptr_test;
    qDebug() << "Displaying &test = " << &pointer;
    qDebug() << "Displaying &ptr_test = " << &ptr_test;

    QString *pointerValue = ptr_test;
    qDebug() << "Display &pointerValue = ptr_test : " << pointerValue;

输出

Displaying orignial variable
Displaying test_string =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &test_string =  0x7fffffffd330

Displaying tests:
Passed with const &_string_
Displaying login =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _login =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &login =  0x5555559a4550
Displaying &_login =  0x7fffffffd330

Passed with const _string_
Displaying auth_tok =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _auth_tok =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &auth_tok =  0x5555559a4560
Displaying &_auth_tok =  0x7fffffffd4c0

Passed address with const _string_
Displaying key =  "This string is created in loginwindow and pointer sent to dialog"
Displaying _key =  "This string is created in loginwindow and pointer sent to dialog"
Displaying &key =  0x5555559a4558
Displaying &_key =  0x7fffffffd4b0

Passed with *ptr_string_
Displaying test =  0x7fffffffd330
Displaying ptr_test =  0x7fffffffd330
Displaying &test =  0x5555559a4568
Displaying &ptr_test =  0x7fffffffcf98

Display &pointerValue = ptr_test :  0x7fffffffd330

1 个答案:

答案 0 :(得分:0)

我找到了我的解决方案(如果有人称之为)。

我需要应用一种名为

的技术
  由@Hayt提供的

dereferencing链接 - 请参阅对问题的评论

虽然“意义/声音”这个词与它实际上没有关联。请参阅提供的链接。

最初我有以下

QString *pointerValue = ptr_test;
qDebug() << "Display *pointerValue = ptr_test : " << pointerValue;

    //Output : Display pointerValue = ptr_test :  0x7fffffffd330    <--- the value contained by '*pointer' which is 0x7fffffffd330
    //note that at address '0x7fffffffd330', the value contained there is the string "This string is created in loginwindow and pointer sent to dialog"

qDebug() << "Display &pointerValue = ptr_test : " << &pointerValue;

    //Display &pointerValue = ptr_test :  0x7fffffffd270            <---the address of '*pointer'

但多亏了post我遇到了这个问题,为了便于阅读,我引用了帮助我解决问题的部分:

  

下一个示例演示了指针的正确用法:

     

的#include     使用namespace std;

void main()
{
  int x;
  int *ptr_p;

  x = 5;
  ptr_p = &x;

  cout << *ptr_p;          < --- note the * to refer to the memory location: added by @KGCybeX
}
     

注意:如果忘记在cout语句中放置*(在指针前面),则将打印整数x的地址。 (试试吧)。

解决方案/答案

因此,我显示包含另一个变量的内存地址的指针的解决方案是:

qDebug() << "Display *pointerValue = ptr_test : " << *pointerValue;
//Display *pointerValue = ptr_test :  "This string is created in loginwindow and pointer sent to dialog"