19

I am little bit confused about pointers and how many bytes they take up. In my textbook it first says that pointers on 16 bit systems take up 2 bytes, 32 bit systems 4 bytes, 64 bit system 8 bytes and so on. Then 10 lines after, it says that pointers take up that many bytes, that are needed to hold the addresses. Here are my questions :

  1. So does this mean that if we are lets say on 64 bit system, address will need at most 8 bytes?
  2. If we are on 16 bit system and pointers take 2 bytes, and address needs more the 2 bytes to be placed then what happens?
alk
  • 69,737
  • 10
  • 105
  • 255
GovernmentFX
  • 307
  • 1
  • 2
  • 5

6 Answers6

20

There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.

For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple char values; all other types take up a full word or more. On such an architecture, a char * and void * would need some extra bits to offset into the word compared to other pointer types.

Note also that a pointer type may be wider than the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.

On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't have to be true.

John Bode
  • 119,563
  • 19
  • 122
  • 198
11

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

On top of that, not every architecture uses a flat memory model (for example, see x86 memory segmentation). This further complicates matters.

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

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

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
7

The size of the pointer basically depends on the architecture of the system in which it is implemented. For example the size of a pointer in 32 bit is 4 bytes (32 bit ) and 8 bytes(64 bit ) in a 64 bit machines. The bit types in a machine are nothing but memory address, that it can hold. 32 bit machines can hold 2^32 and 64 bit machines can hold 2^64 address spaces. So a pointer (variable which points to a memory location) should be able to point to any of the memory address (2^32 for 32 bit and 2^64 for 64 bit) that a machines holds.

Because of this reason we see the size of a pointer to be 4 bytes in 32 bit machine and 8 bytes in a 64 bit machine.

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

Community
  • 1
  • 1
Rndp13
  • 1,094
  • 1
  • 21
  • 35
4

Pointers are used to store the address of a variable.The width of the memory address/location depends on the computer architecture.If the computer architecture is 16-bit then it means that it can have 2^16 memory locations.Therefore, for a pointer to be able to store any memory location in this computer it should be 16 bits wide,i.e, 2 bytes(8 bits=1 byte). Similarly for 32-bit and 64-bit architecture we need to have pointers with size 4 bytes(32-bit width) and 8 bytes(64-bit width) respectively.
Also if a system is 16-bit then it cannot have address location of size more than 16 bits.

1

This will tell you how many bytes it takes to represent a pointer on your system.

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

Here's a compiler flag you can play with that's on-topic:

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

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

Assumption, you're on a 64-bit system with GCC. Hope this clarifies things a bit.

Darren Stone
  • 2,008
  • 13
  • 16
  • Pointers hold addresses, and they themselves have addresses, so not quite the same thing. Also 16-bit systems can have more than 64K of memory, so your second statement is false. – Mark Tolonen Dec 24 '13 at 16:51
  • @MarkTolonen, yes, good point. My intent was to guide @GovernmentFX away from a notion that *"pointers take 2 bytes, and address needs more the 2 bytes."* This is what I meant by, "the pointers **are** the addresses and that's all." In order to point to an `int`, for example, one needs only a pointer. *That* is the address. Thanks though, because OP should also understand that one can have an addresses to an address to an address, etc. – Darren Stone Dec 24 '13 at 16:59
  • @MarkTolonen, yes, one can go beyond C language features and use libraries or other techniques to address any amount of memory on a 16-bit system. However, I see the context of this question as a pure C and pointers question, since that's what it's tagged. I stand by my statement that with a 16-bit address, one can address 2^16 bytes of memory *in C*. It's too close to Christmas now so I'll concede and I have edited my answer to keep it simple for OP to, hopefully, learn a bit more about how pointers are stored and how he can verify this, hands-on. Cheers. :-) – Darren Stone Dec 24 '13 at 17:14
0

I will try to give you a rough idea. But there are exceptions to the "rule".

  1. So does this mean that if we are lets say on 64 bit system, address will need at most 8 bytes?
  • Yes if it is a 64bit system, it means that (in most cases) there are 64bit representing the RAM Main Memory Addresses.
  1. If we are on 16 bit system and pointers take 2 bytes, and address needs more the 2 bytes to be placed then what happens?
  • I assume you refer at allocating memory cells. If that is the case, then you can allocate as much memory as you like, this will not be a problem. For example if your system has a 16bit Pointer Address then you can allocate 512bits.
costik2020
  • 36
  • 2