Reputation: 9
How to get pointer to self object inside the class? Simple code below shows the creation of two objects: A and B. A contains pointer to B that must me assigned in B's constructor.
#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;
};
Example output of this code seems to be logically incorrect:
constructor B addr = 140737243484752
stack B addr = 140737243483712
B.value = 12
So, is there any variant to get pointer to an object inside itself?
Upvotes: 1
Views: 1306
Reputation: 7863
The issue is that you're creating multiple B
instances. The first instance is at the beginning of main
: B b;
. A few lines later, you construct another B
(B(&a)
), then assign it to b
. Your output is correct since constructor B addr
is being output from the temporary B
's constructor, not the B
you're actually keeping around in main
.
By eliminating the temporary, I get the output you seem to be expecting:
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;
};
Possible output (addresses will obviously differ).
constructor B addr = 140731920033760
stack B addr = 140731920033760
B.value = 10
Also, you technically have an invalid write in the original code since a.ptr
has the address of a temporary. If you compile with the address sanitizater, you'll get some useful output:
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
Upvotes: 5