c++ - How does returning values from a function work? -
i had serious bug, forgot return value in function. problem though nothing returned worked fine under linux/windows , crashed under mac. discovered bug when turned on compiler warnings.
so here simple example:
#include <iostream> class a{ public: a(int p1, int p2, int p3): v1(p1), v2(p2), v3(p3) { } int v1; int v2; int v3; }; a* geta(){ a* p = new a(1,2,3); // return p; } int main(){ a* = geta(); std::cerr << "a: v1=" << a->v1 << " v2=" << a->v2 << " v3=" << a->v3 << std::endl; return 0; }
my question how can work under linux/windows without crashing? how returning of values done on lower level?
on intel architecture, simple values (integers , pointers) returned in eax
register. register (among others) used temporary storage when moving values in memory , operand during calculations. whatever value left in register treated return value, , in case turned out wanted returned.
Comments
Post a Comment