I am not very familiar with pointers, since i mostly do java, and java has no pointers and now i am learning C++. In a C++ tutorial, in order to know the size of memory occupied by a variable, the tutor used size of on a pointer to the variable, i.e,
int v = 23;
int *p = &v;
cout << sizeof(p) << endl;
This got me confused because in my first year i was taught in C programming that i will need to do sizeof on the variable itself. So i first concluded that they meant the same. But i when i tried on my computer i had different results. I have the following code...
#include <iostream>
int main()
{
int *ptr = new int;
int n = 23;
ptr = &n;
std::cout << sizeof(n) << std::endl;
std::cout << sizeof(ptr) << std::endl;
return 0;
}
When i run the above code, i get output of 4, and 8. But my friend compiled and executed the same code on his machine and he had output of 4 and 4. I don't know why this is happening and why the tutor used sizeof on a pointer to the variable instead of on the variable itself, since he wanted to know the amount of memory occupied by that variable. I know variables in C/C++ have different memory capacities because of different architectures, atleast that is what i was taught in C. That an int in 64bit machine has a different size from that on a 32bit machine. But i though my results had to be at least consistent i.e 8 and 8, or 4 and 4. I am using a 64bit architecture and a 64bit OS, my friend is using a 64bit architecture with a 32bit OS.