We are learning on our medium-term facility on Tuesday.
Our professor has posted some training materials on the Internet, including the following:
"In addition, you should be able to draw a memory diagram with some code, for example:
void foo( int &x ) { x = 1000; } void bar( int *x ) { *x = 1000; } void foobar( int x ) { x = 1000; } int main() { int x = 1234; int &y = x; int *z = &x; int array_1[5]; int *array_2[5]; array_1[0] = 10; array_2[0] = (int*)10; array_2[1] = &y; array_2[2] = &x; foo( x ); foo( y ); foo( *z ); bar( &x ); bar( &y ); bar( z ); foobar( x ); foobar( y ); foobar( *z ); return 0; }
We try to go through it one step at a time to find out what is allocated on the stack, what is allocated on the heap, and what is the value of each thing.
We do not understand: & y contains the address x, but & y = & x ... so what is the address y? For example, does the stack need to stack y
user114518
source share