I'm doing a few experiments to solve an exercise problem from The C++ Programming Language (4th edition), Stroustrup.
Here's my code:
#include <iostream>
using std::cout;
using std::endl;
struct arr_holder {
int mem_arr[];
};
int main()
{
int lcl_arr[10];
arr_holder ah;
cout << &lcl_arr[0] << endl;
cout << &ah.mem_arr[0] << endl;
return 0;
}
The program prints two different addresses of lcl_arr
and ah.mem_arr
when compiled on my Linux g++ (4.9.2), like the following:
$ g++ foo.cc && ./a.out
0x7ebe4670
0x7ebe4698
However, when compiled on MinGW g++ (5.3.0), they're the same:
C:\sandbox>g++ foo.cc && a.exe
0x60fef8
0x60fef8
So, obviously, writing to either lcl_arr
or ah.mem_arr
would result in writing to the same array because they point to the same address, which I find to be quite surprising, because I think a local array (lcl_arr
) and a member array (ah.mem_arr
) are totally different things.
Can someone explain to me why they get the same address location on MinGW?