-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathglobals_lca_5.cpp
More file actions
45 lines (33 loc) · 895 Bytes
/
globals_lca_5.cpp
File metadata and controls
45 lines (33 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>
// struct Foo {
// int x;
// Foo(int x) noexcept : x(x) {}
// ~Foo() noexcept {
// ++x;
// printf("x: %d\n", x);
// x = 0;
// }
// };
// Foo createInitialFoo() noexcept {
// int x = 42;
// return Foo(x);
// }
// Foo foo = createInitialFoo();
// int main() { printf("x: %d\n", foo.x); }
// struct Foo is not an integer, so the LCA ignores it...
// => Create an artificial example that models the same behaviour and memory
// layout as above
using Foo = int;
Foo createFoo() noexcept { return 42; }
void Foo_dtor(Foo &_this) noexcept {
++_this;
printf("x: %d\n", _this);
_this = 0;
}
extern "C" int __cxa_atexit(void (*dtor)(void *), void *, void *);
Foo foo;
__attribute__((constructor)) void makeGlobalFoo() {
foo = createFoo();
__cxa_atexit((void (*)(void *))&Foo_dtor, &foo, nullptr);
}
int main() { printf("x: %d\n", foo); }