-
-
Save Milek7/1100114e5fbe2864f6ee15acb06a2c29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
struct SaveLoad { | |
bool global; | |
void *address; | |
}; | |
static inline void *GetVariableAddress(const void *object, const SaveLoad *sld) | |
{ | |
return const_cast<unsigned char *>((const unsigned char*)(sld->global ? nullptr : object) + (ptrdiff_t)sld->address); | |
} | |
struct Settings { | |
int a; | |
int b; | |
int c; | |
int d; | |
}; | |
Settings settings; | |
void setup1() __attribute__ ((optnone)); | |
struct SaveLoad *setup2() __attribute__ ((optnone)); | |
void setup1() { | |
settings.a = 11; | |
settings.b = 22; | |
settings.c = 33; | |
settings.d = 44; | |
} | |
struct SaveLoad *setup2() { | |
SaveLoad *sl = new SaveLoad(); | |
sl->global = true; | |
sl->address = &settings.c; | |
return sl; | |
} | |
int test1() { | |
setup1(); | |
SaveLoad *sl = setup2(); | |
void *ptr1 = GetVariableAddress(nullptr, sl); | |
return *((int*)ptr1); | |
} | |
int test2() { | |
setup1(); | |
SaveLoad *sl = setup2(); | |
void *ptr1 = GetVariableAddress(&settings, sl); | |
return *((int*)ptr1); | |
} | |
#include <stdio.h> | |
int main() | |
{ | |
printf("%d, %d\n", test1(), test2()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment