what would happen if you had 2 variables in the same memory location? my theory is that it would cause the b.s.o.d. any way i was working on trying to do this and i came up with this little code
| CODE |
#include <iostream> using namespace std;
int main() {
const int &num1=0x76fde0; const int &num2=0x76fde0;
cout<<num1<<" "<<num2; cout<<"\n\n"; system("pause"); return 0; } |
now for some reason a couple of the same numbers are outputted and i have no idea where they come from unless 0x76fde0 is something else than a memory location..... i dunno im having fun with it anyway.. its good to do somewhat useless things once in a while B)
>> what would happen if you had 2 variables in the same memory location
The second would likely overwrite the first.
It's actually very simple. With a bit of imagination this is your memory:
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
And now we fillin a integer value(red)
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
And now we fill in a double value(green)
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Now if we try to read the integer value we are going to read 4 bytes from where the integer starts. So the 1 byte left of the integer and the 3 first byte of a double. If we now change the value of the integer we would get this:
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
And the value of the double will have been changed