在类中获取指向self的指针

1 c++ pointers c++11

如何在类中获取指向self对象的指针?下面的简单代码显示了两个对象的创建:A和B. A包含指向B的指针,必须在B的构造函数中指定.

#include<iostream>

using namespace std;

class B;
class A {
    public:
    B* ptr;
};

class B {
    char array[1024];
    public:

    int value = 12;

    B() {};

    B(A* a) {
        a->ptr = this;
        cout << "constructor B addr = " << reinterpret_cast<std::uintptr_t>(this) << endl;
    }
};

int main() {
    B b;
    A a;

    b = B(&a);

    cout << "stack B addr       = " << reinterpret_cast<std::uintptr_t>(&b) << endl;

    a.ptr->value = 10;
    cout << "B.value            = " << b.value << endl;

    return 0;
};
Run Code Online (Sandbox Code Playgroud)

此代码的示例输出似乎在逻辑上不正确:

constructor B addr = 140737243484752
stack B addr       = 140737243483712
B.value            = 12
Run Code Online (Sandbox Code Playgroud)

那么,是否有任何变体可以获取指向其自身内部对象的指针?

Ste*_*ell 5

问题是您正在创建多个B实例.第一个例子是在开头main:B b;.几行后,你构造另一个 B(B(&a)),然后分配给它b.你的输出是正确的,因为constructor B addr从临时B的构造函数输出,而不是B你实际上保持输入main.

通过消除临时性,我得到你似乎期待的输出:

int main() {
    A a;
    B b(&a);

    cout << "stack B addr       = " << reinterpret_cast<std::uintptr_t>(&b) << endl;

    a.ptr->value = 10;
    cout << "B.value            = " << b.value << endl;

    return 0;
};
Run Code Online (Sandbox Code Playgroud)

可能的输出(地址明显不同).

constructor B addr = 140731920033760
stack B addr       = 140731920033760
B.value            = 10
Run Code Online (Sandbox Code Playgroud)

此外,您在技术上在原始代码中a.ptr具有无效写入,因为具有临时地址.如果使用地址sanitizater进行编译,您将获得一些有用的输出:

constructor B addr = 140732509397056
stack B addr       = 140732509395968
=================================================================
==3080==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffed73b2440 at pc 0x55ae5d0dc4c2 bp 0x7ffed73b1b90 sp 0x7ffed73b1b80
WRITE of size 4 at 0x7ffed73b2440 thread T0
    #0 0x55ae5d0dc4c1 in main /tmp/so/addr.cpp:32
    #1 0x7f2cdc4d0ae6 in __libc_start_main (/lib64/libc.so.6+0x21ae6)
    #2 0x55ae5d0dc149 in _start (/tmp/so/add+0x1149)

Address 0x7ffed73b2440 is located in stack of thread T0 at offset 2208 in frame
    #0 0x55ae5d0dc239 in main /tmp/so/addr.cpp:25

  This frame has 3 object(s):
    [32, 40) 'a'
    [96, 1124) 'b'
    [1184, 2212) '<unknown>' <== Memory access at offset 2208 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-scope /tmp/so/addr.cpp:32 in main
Shadow bytes around the buggy address:
  0x10005ae6e430: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
  0x10005ae6e440: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
  0x10005ae6e450: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
  0x10005ae6e460: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
  0x10005ae6e470: f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8 f8
=>0x10005ae6e480: f8 f8 f8 f8 f8 f8 f8 f8[f8]f2 f2 f2 00 00 00 00
  0x10005ae6e490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005ae6e4a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005ae6e4b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005ae6e4c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005ae6e4d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==3080==ABORTING
Run Code Online (Sandbox Code Playgroud)