sizeof a pointer

浪子不回头ぞ 提交于 2019-12-10 10:54:28

问题


int i=0;
int *p = &i;
std::cout<<sizeof(i)<<" vs "<<sizeof(p)<<"\n";

char c='0';
char*pc = &c;
std::cout<<sizeof(c)<<" vs "<<sizeof(pc)<<"\n";

double d=0.123456789;
double *pd = &d;
std::cout<<sizeof(d)<<" vs "<<sizeof(pd)<<"\n";

Why is the size of a pointer always equal to that of an integer which is 4?


回答1:


Because that is how much memory a pointer takes: 4 bytes.

If this was a 64-bit application, it would return 8.




回答2:


Pointers don't contain the data they point to.

Pointers just point to the data they point to.

sizeof( double* ) is the size of the data used to describe where a double is located. Much as a piece of paper with a house address on it doesn't change size if the house is bigger or smaller, the size of a pointer is not a function of the size of the data pointed to.

Well, usually. You could imagine international addresses being longer, and requiring more paper. Similarly, you could imagine addresses inside an apartment building requiring an extra line for "unit number" compared to addresses elsewhere, or even "in-apartment" addresses that only contain the "unit number" and are thus shorter. The standard allows the size of pointers to various types to vary, and some architectures (8086 most famously) has the concept of both near and far pointers (being 16 and 32 bits respectively).

This isn't common anymore (except maybe with member function pointers, but how common are those?)




回答3:


A pointer is actually a variable containing an address. On a 32-bit machine, the address is always 32-bit, so it's 4 bytes. The same logic for 16-bit or 64-bit machines.

Just looked into the C99 standard and it says:

The size of a pointer is not necessarily the same as the size of any integer type. An implementation may support more than one size of pointer.

Well, thus I think the safest way to know a pointer's size is sizeof().




回答4:


A pointer will have a size depending on the kind of application you're compiling.

A pointer is like a 32-bit unsigned int in a 32-bit application because it stores a memory location.

If the application is an N-bit application, the size of any pointer will be N bits long because it needs to be large enough to store the memory location.

Any N-bit computer that can run N-bit applications will have a RAM limit of 2^N bytes. This makes the largest memory location id equal to 2^N - 1 (because 0x00000000 is a valid memory address).

2^N - 1 requires N bits to be represented in binary, and that my friend is why a pointer has a size of 4 bytes in a 32-bit application.

Take a 3-bit application for simplicity as an example: (I'd rather not deal with large numbers)

Let's assume it's running on a 3-bit machine. Since 2^3 - 1 is the greatest number that can be represented in binary with 3 bits, then the max memory that the system could handle would be 2^3 bytes. (These are unsigned. Negative memory addresses don't exist)

The possible memory addresses would be:

  • 0x0 (Binary representation: 000)
  • 0x1 (Binary representation: 001)
  • 0x2 (Binary representation: 010)
  • 0x3 (Binary representation: 011)
  • 0x4 (Binary representation: 100)
  • 0x5 (Binary representation: 101)
  • 0x6 (Binary representation: 110)
  • 0x7 (Binary representation: 111)

Thus, a pointer pointing to a memory address would need to have 3 bits in such a case.

Replace 3 with 32 and this would apply to what you're doing here.

(Overkill answer much? Yes. Why? Because I was bored.)



来源:https://stackoverflow.com/questions/15538210/sizeof-a-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!