首页 > 解决方案 > 在类中获取指向自身的指针

问题描述

如何在类中获取指向自身对象的指针?下面的简单代码显示了两个对象的创建: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;
};

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

constructor B addr = 140737243484752
stack B addr       = 140737243483712
B.value            = 12

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

标签: c++c++11pointers

解决方案


问题是您正在创建多个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;
};

可能的输出(地址显然会不同)。

constructor B addr = 140731920033760
stack B addr       = 140731920033760
B.value            = 10

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

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

推荐阅读