How many bytes do pointers occupy?

I am a bit confused about pointers and the number of bytes they use. My tutorial first says that pointers on 16-bit systems take 2 bytes, 32-bit systems 4 bytes, 64-bit systems 8 bytes, and so on. Then 10 lines, it says that pointers take up as many bytes that are needed to store addresses. Here are my questions:

  • Does this mean that if we say that on a 64-bit system, the address will have no more than 8 bytes?
  • If we are on a 16-bit system, and the pointers take 2 bytes, and the address needs more than 2 bytes to be placed, then what happens?
+6
source share
5 answers

There is no fixed answer; it depends entirely on the architecture, implementation of the compiler, and even on the type of the pointer itself. Pointers to different types cannot guarantee the same size and / or presentation.

For example, suppose a destination architecture where the smallest addressable storage unit is 16 bits wide (or wider). Each word can contain multiple char values; all other types occupy a full word or more. In such an architecture, char * and void * will require additional bits for word offsets compared to other types of pointers.

Note also that the type of pointer can be wider than the number of bits actually needed to store the address. The original Macintosh was powered by a Motorola 68000 processor, which had a 32-bit word size, but only 24 bits on the address bus. The pointer types were 32 bits wide, leaving the top 8 bits unused. Entrepreneurial MacOS programmers have taken advantage of this to store some data in the topmost byte of type pointer, using most of this precious 128 KB of RAM. Of course, Motorola eventually released a processor with 32 address lines (68020), which means that all this code had to be rewritten.

On modern commercial desktop and server hardware (read: x86), it’s reasonable to assume that all types of pointers are the same size as the native word size (32- or 64-bit), and that all types of pointers have the same size and presentation . Just keep in mind that this should not be true.

+6
source

The short answer is that it depends. When we say that the system is 32-bit, this may mean that the native integer is 32 bits wide, that the e-mail address (that is, the size of the pointer) is 32 bits wide, or both.

In addition, not every architecture uses a flat memory model (for example, see x86 memory segmentation ). This further complicates the situation.

It is best to treat the size of the pointer as opaque.

C99 provides tools in the form of intptr_t and uintptr_t , which are integers that are guaranteed to be wide enough to hold a pointer.

+10
source

The size of the pointer mainly depends on the architecture of the system in which it is implemented. For example, a pointer size in 32-bit format is 4 bytes (32 bits) and 8 bytes (64 bits) on 64-bit machines. The types of bits in a machine are just the memory address that it can hold. 32-bit machines can contain 2^32 , and 64-bit machines can contain 2^64 address spaces. Therefore, a pointer (a variable pointing to a memory location) must point to any of the memory addresses ( 2^32 for 32 bit and 2^64 for 64 bit ) that are stored in the machines.

For this reason, we see that the size of the pointer is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.

Answer in Is sizeof (some pointer) always equal to four?

+4
source

This will tell you how many bytes are required to represent the pointer on your system.

  #include <stdio.h> int main() { printf("%ld bytes per pointer\n", sizeof(void *)); } 

Here is the compiler flag that you can reproduce on this topic:

 $ gcc -m32 -o prog32 prog.c $ gcc -m64 -o prog64 prog.c 

The first line generates a binary for the 32-bit environment, giving you 4 byte pointers. The second line generates binary for a 64-bit environment, giving you 8 byte pointers. You can confirm this by running the above program.

Suppose you are using a 64-bit system with GCC. Hope this clarifies things a bit.

+1
source

Pointers are used to store the address of a variable. The width of the memory address / location depends on the architecture of the computer. If the computer architecture is 16-bit, this means that it can contain 2 memory cells 16. Therefore, for the pointer to store any memory cell on this computer, it must be 16 bits wide, that is, 2 bytes (8 bits = 1 byte ) Similarly, for 32-bit and 64-bit architecture, we need pointers of 4 bytes (32-bit width) and 8 bytes (64-bit width), respectively.
In addition, if the system has a 16-bit version, it cannot have an address location larger than 16 bits.

+1
source

All Articles