#include <stdio.h>
#include <stdlib.h>
void **ptr;
size_t last;
size_t init;
// Function to save values for later. Call
// with init object to init
size_t save_static(void *obj) {
if(obj == &init) {
if(ptr) free(ptr);
ptr = calloc(sizeof *ptr, init);
return 0;
}
last = -1;
while(ptr[++last])
if(ptr[last] == obj)
break;
ptr[last] = obj;
return last;
}
int main() {
init = 10000;
save_static(&init);
for(int i=0; i<10; i++) {
// Ops! Too late we realize that
// we need to save the last value
// of i for later. No worries!
static int x;
x = i;
save_static(&x);
}
printf("i: %d\n", *(int*)ptr[last]);
for(int i=0; i<12; i++) {
for(int j=0; j<118; j++) {
// More than one variable?
// No problemo!
static int x, y;
x = i;
y = j;
static int arr[2];
arr[0] = save_static(&x);
arr[1] = save_static(&y);
save_static(arr);
}
}
int *arr = (int*)ptr[last];
int i = *(int*)ptr[arr[0]];
int j = *(int*)ptr[arr[1]];
printf("i: %d j: %d\n", i, j);
}