Classified Classified Classified Classified -4 years ago 151
C++ Question

C++ Pointers take up more memory than an integer?

I thought that pointers took up less memory, and were more efficient but when I use sizeof() on a pointer, it says it uses twice the memory as an integer. What am I missing here? Here's my code

int p = 1;
Int a = 1;
Int *pointera;
pointera= &p;
cout << sizeof(pointera) << endl;
cout << sizeof(a) << endl;

It says "a" takes up 4 bytes and "pointera" takes up 8. Shouldn't the pointer tak up less? What am I missing

Answer Source

pointer contains a memory address. When you call the sizeof on a pointer it gives the size of the address. In a 64 bit computer a memory address would be 8 bytes.


Int *pointera = &p;

Here pointera stores the memory address of p. This address is 64 bit which means 8 bytes.

size of int, double and char are different but if you calculate the size of pointer of these data types than they would all be of same size in the same computer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download