gpt4 book ai didi

c++ - 在类中获取指向自身的指针

转载 作者:行者123 更新时间:2023-11-30 01:03:04 37 4
gpt4 key购买 nike

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

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

最佳答案

问题是您正在创建多个 B 实例。第一个实例在 main 的开头:B b;。几行之后,您构建了另一个 B (B(&a)),然后将其分配给b。您的输出是正确的,因为 constructor B addr 是从临时 B 的构造函数输出的,而不是您实际保留的 B 主要

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

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 具有临时地址,因此从技术上讲,您在原始代码中的写入无效。如果您使用地址 sanitizer 进行编译,您将获得一些有用的输出:

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

关于c++ - 在类中获取指向自身的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853066/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com