-2

What is meant by the size of a pointer? Shouldn't the size of pointer depend on the type? Most of the sources say the size of a pointer if 4 or 8 bytes. I need some clarity on this claim.

  • 1
    I think you're confusing the size of a pointer with the size of the thing it points to. – Joseph Sible-Reinstate Monica Apr 04 '20 at 05:33
  • 2
    `sizeof (a_pointer)` is fixed for the hardware and OS. For example on x86, the size of a pointer is 4-bytes. (32.bits) On x86_64 it is 8-bytes (64-bits). Now pointer arithmetic is determined by the type of the pointer. For example `char *p` (a char is 1-byte) so `p++` advances the address by 1-byte. For `int *p` (a pointer to `int`) `p++` advances the pointer to the next `int` 4-bytes from the current address. – David C. Rankin Apr 04 '20 at 05:33
  • 5
    It is not true. The standard doesn't specify equal and fixed pointer sizes. There are exotic platforms, where sizeof(char*) != sizeof(int*). – 273K Apr 04 '20 at 05:37
  • Yes, there are all types of pink-elephant and purple-poka-dot systems out there, but my comment related to specifically x86 and x86_64. Perhaps you would be so kind as to identify the system you are referring to so we can discuss its exception in detail? – David C. Rankin Apr 04 '20 at 05:44
  • 1
    Does this answer your question? [Are there any platforms where pointers to different types have different sizes?](https://stackoverflow.com/questions/916051/are-there-any-platforms-where-pointers-to-different-types-have-different-sizes) – Tom Karzes Apr 04 '20 at 06:52
  • Does this answer your question? [Size of pointers: Dependent factors](https://stackoverflow.com/questions/13109666/size-of-pointers-dependent-factors) – RobertS supports Monica Cellio Apr 04 '20 at 07:33
  • There are already tones of answers on SO for your concerns. Just search for it and you´ll get your answer very soon. If you have a specific question about a particular point, ask a new detailed question. – RobertS supports Monica Cellio Apr 04 '20 at 07:36
  • @DavidC.Rankin: So what if it only happens in an “exotic” system. They exist nonetheless, and some people still have to deal with them. Your comment alleged the size of a pointer is fixed by the hardware and the OS without restricting that statement to x86. As a matter of fact, it is false. You may wish to confine your practice to x86, but others do not, and you should not spread false statements. (Additionally, your statement is false even for x86. The size of a pointer is determined by the C implementation, not the hardware or the OS.) – Eric Postpischil Apr 04 '20 at 10:24
  • @DavidC.Rankin: Simple, you write statements that describe the typical situation while stating it is typical, not universal. It is not hard to say “Most modern C implementations use a single size for all pointers.” It is clear, it conveys the typical situation, and it obviously conveys the fact that not all C implementations necessarily do this, while at the same time is does not burden the student with details they do not need at this point. Instead of planting a false idea in their head, you have given them fair notice they have more to learn later. And it has the quality of being true. – Eric Postpischil Apr 04 '20 at 23:03
  • @DavidC.Rankin: As I wrote, your statement “sizeof (a_pointer) is fixed for the hardware and OS” did not restrict itself to x86. In fact, the following statement says that x86 is an example, implying that the prior sentence is a rule beyond x86. Even if it were, in your mind, restricted to x86, students are readers, not telepaths—they learn from the words. That first sentence is false, as a matter of fact, on two different grounds, and so it teaches students the wrong thing. And every wrong thing learned must be later unlearned, which takes more effort. It is unfair to them. – Eric Postpischil Apr 04 '20 at 23:05
  • Look, my point isn't to pick a fight over what the verbiage should be. My point is to help a new C programmer understand how big `a_pointer` is. Whether you say it is defined by the C implementation or it is set for specific hardware, you are looking at the same thing from both ends of the cognitive taxonomy scale. The point being for x86 pointers are 4-bytes for x86_64 they are 8-bytes. That covers the answer to 99.99% what a new programmer will every see. Yes, there are exceptions to any rule, and if you would like to point them out, please do. Let's move on. – David C. Rankin Apr 05 '20 at 03:41

2 Answers2

3

For size of a pointer I would mean the number of bits (or bytes) necessary to hold that pointer in memory, or send its value across some channel, and this is (possibly) different from the size of the object the pointer points to.

Then, it can be assumed as fairly true the affirmation that pointer sizes are commonly 32 or 64 bits (4 or 8 bytes), in the sense that the systems much talked about (computers, smartphones and tablets) have pointers of that size.

But there are other systems around, smaller like DOS-based PCs or microcontrollers for embedded systems, where a pointer can be 16 bits wide or even less, and bigger systems with bus width of, say, 128 bits.

I worked in the past with the Intel 8051 CPU, which had pointers 8 bits wide, 16 bits wide, and 24 bits wide. Of course they were not freely mixable... That CPU was indeed quite strange, having about 3-4 different (and little) areas of memory; a "specialized" pointer could point only in its special area, while the 24 bit wide one could point to any area because in the upper byte there was a "selector".

Another matter is the size of the object the pointer points to. On normal computers it is a byte, but sometimes, on certain systems, it is impossible to address bytes on odd addresses in this way, so pointer arithmetic gets complicated. The 8051 (I like it!) had even pointers pointing to bits! So the size of the pointed object was actually an eight of byte, and incrementing the pointer by one could, or could not, address a different memory location than before.

1

Data is stored in memory. That memory has an address. Pointers hold the memory address for where the data starts.

Specifically, pointers usually hold the address of the "first byte" of data where the type resides (note that technically, the first byte might contain the last bits of data, depending on endianness).

i.e., if a long double is 128bit (16 bytes), the pointer value will point to the first byte and the pointer type will indicate the numbers of bytes that should be read.

Should you "cast" the long double pointer in the example to an int * (an int pointer), only sizeof(int) bytes would be read - but the value, the address of the first byte, will remain the same.

Hence, the pointer value is oblivious to the size of the data, the pointer only needs to be large enough to contain the address of the first byte. For this reason, usually pointers have the same length which is derived from a computer's "address space".

It is very similar to a catalog card in a library. Just like a "book address" in a library depends on the size of the library, the pointer value (the memory address) depends on the size of the computer's "address space", not the size of the type.

On most 32 bit and 64 bit CPUs, the address space is limited to either 32 or 64 bits. However, some systems have special address spaces for special pointers (such as function pointers)... this is mostly obsolete. It was more in use when CPUS were smaller than 32 bits and the "address space" was limited.

Note that values in the address space (pointers) can point to any location on the hardware (usually a byte in memory, but sometimes a register or a piece of hardware)... this is why the OS (kernel), leveraging some hardware support, will usually expose a "virtual" address space per process, shielding the hardware and other processed from a misbehaving process.

P.S.

I loved the answer given by @linuxfansaysReinstateMonica ... However, I found that I wanted to clarify some of the information in that answer. You should really read it. This answer is mostly a clarification for their answer.

Myst
  • 18,516
  • 2
  • 45
  • 67