Dear Colleagues! I want to understand how the stack works, using the example of a function of three variables.
void f2() { int B = 5; int *pB = &B; // задание адреса в указатель int &sB = B; //задание объекта в ссылку cout << "addres of B = " << &B << "\n"; cout << "value of B = " << B << "\n"; cout << "addres of pB = " << &pB << "\n"; cout << "value of pB = " << pB << "\n"; cout << "addres of sB = " << &sB << "\n"; cout << "value of sB = " << sB << "\n"; } int main() { f2(); system("PAUSE"); return 0; } Output:
addres of B = 0038F644 value of B = 5 addres of pB = 0038F638 value of pB = 0038F644 addres of sB = 0038F644 value of sB = 5 1. Neponyatka between the first and third line. In the address space, as I thought, the address of the newer variable should be larger than the old one. In practice:
`0038F638 - 0038F644 = -C;` That is, a shift of 12 positions back. Although one of the programmers with great experience, said that the shift will go 4 positions forward - the dimension of int , which is expressed in bytes.
2. Address sB - generally coincided with the address B ! And the value of sB coincided with the value of B ! It seemed to me obvious that the value placed in sB should be equal to address B And the address sB must be shifted four positions ( sizeof(int) ) forward, relative to the address of the previous variable.
Questions:
What can explain the observed behavior? How does (in fact) assign the address of the next variable relative to the previous one? Is it possible to print the stack in a more literate way than I did? (I want to see a stack in the form of three entries - type - value - address ).
#define Info(v) std::cout << #v << "\thas type: " << typeid(v).name() << " value: [" << (v) << "] addr: " << (void *)&(v) << '\n'(call for each variable) - avp