background
typedef struct Result {
int type;
union {
Err err;
Table tbl;
};
} Result;
typedef struct Err {
int status;
String command;
String err;
} Err;
Result *getres(...) {
....
return &(Result) {....};
}
void somefun(..) {
...
Result *res = getres(..);
// (A)
handleres(res);
}
void handleres(Result *res) {
// (B)
...
}
(A) at point A, res has the expected data passed from getres, but point (B) has different data, and debugging the code view that most of res elements pointers has the same pointer of res!!, not just the first element, except one element res->err->err;
in assembly sense
getres return pointer to local Result in eax, which mapps to span -start(ebp) to -end(ebp) of getres routine.
question
dynamic allocation works, but why statically defined struct pointer behave that way, does assembly frames gets used and -start(ebp), -end(ebp) get overwritten?