View Full Version: what would happen

C++ Learning Community > Computer Lab > what would happen


Title: what would happen
Description: 2 pointers to same thing and diff value


ih8censorship - August 2, 2003 11:58 PM (GMT)
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)

TheHawgMaster - August 3, 2003 06:54 PM (GMT)
QUOTE

#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;
}


I thnk you meant
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;
}

or
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;
}

Shadow of the Moon - August 26, 2003 11:42 PM (GMT)
>> what would happen if you had 2 variables in the same memory location

The second would likely overwrite the first.

dr voodoo - August 27, 2003 07:59 AM (GMT)
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




* Hosted for free by InvisionFree