c++ - location of variables on the stack w/o using (&) -


i'm trying determine way test location of variables on stack (without doing common sense way).

   int main()     {       int i,j;       return 0;     } 

there's no difference between stating int i; intj; , int i,j; can tell cout'ing addresses. goes on stack, j goes on stack. there anyway tell w/o using memory address operator (or pointers).

this specific assignment. can comprehend reason why stack implemented way. can determine of how located (and yes it's compiler specific gcc simplicities sake).

so yes meant doing things hard way here.. figure out , demonstrate why , how stacks implemented in particular manner

i can add more code, more functions.. whatever.. point demonstrate ordering on stack without &

so want find addresses of local variables without using c operator designed find address of variable. why?

it's worth pointing out that, in modern compiler/processor combination, , j in example may not on stack if don't use & operator. if them compiler doesn't optimise them away, may still choose put them in registers.


edit: i've read comment datenwolf's answer , think know being asked for. here possible answer, must stress making big assumptions compiler. in particular,

  • all variables stored on stack
  • variables aren't optimised away (turn off optimisations in compiler).
  • storage space automatic arrays allocated directly on stack
  • the stack isn't cleaned between function calls

if have function this:

int f() {     int = 1, j = 2;     return 0; } 

you can inspect order of i, j in memory function:

void order() {     int a[2];     if (a[0] == 1)     {         printf("i first\n");     }     else     {         printf("j first\n");     } }  int main() {     f();     order();     return 0; } 

never use above in real code. relies on several pieces of officially undefined behaviour.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -