C
Chris Schumacher
I wrote the following program to demonstrate an oddity I found in C++.
#include <iostream>
using namespace std;
int main()
{int *p, *q;
p = q;
q = new int;
*q = 12;
cout << *p;
system("pause");
return 0;
}
Your compiler will either give a gibberish answer or crash when you try
to run this program.
Here's what I don't understand: p and q are pointing to the same memory
address. That being said, why can't you access the dynamic variable using
p as well as q?
What would happen if you created a new variable using p too? Aren't they
both pointing to the same memory? Would they overwrite each other?
Thanks.
-==Kensu==-
I actually came across this while working with a homemade linked list.
You can imagine how frustrating THAT was...
#include <iostream>
using namespace std;
int main()
{int *p, *q;
p = q;
q = new int;
*q = 12;
cout << *p;
system("pause");
return 0;
}
Your compiler will either give a gibberish answer or crash when you try
to run this program.
Here's what I don't understand: p and q are pointing to the same memory
address. That being said, why can't you access the dynamic variable using
p as well as q?
What would happen if you created a new variable using p too? Aren't they
both pointing to the same memory? Would they overwrite each other?
Thanks.
-==Kensu==-
I actually came across this while working with a homemade linked list.
You can imagine how frustrating THAT was...