Sam
Sam

Reputation: 2347

Size of pointers to pointers in memory

Just a quick question:

on a 32 bit machine, is a pointer to a pointer (**p) going to be 4 bytes?

Upvotes: 1

Views: 119

Answers (5)

Alexey Frunze
Alexey Frunze

Reputation: 62048

Others have already mentioned that it's most certainly 32 bits or 4 8-bit bytes.

However, depending on the hardware and the compiler it may be less or more than that.

If your machine can address its memory only as 32-bit units at 32-bit boundaries, you will have to have a bigger pointer to address and access 8-bit portions (chars/bytes) of every 32-bit memory cell. If the compiler here decides not to have pointers of different sizes, all pointers (including pointers to pointers) become 34+-bit long.

Likewise, if the program is very small and can fit into 64KB, the compiler may be able to reduce all pointers to 16 bits.

Upvotes: 1

Gene
Gene

Reputation: 46960

The logic is that pointers are merely memory addresses. The memory address of any stored entity in a machine with 32-bit addresses is almost certainly 4 bytes. Therefore the memory address of a stored pointer is 4 bytes. Therefore a pointer to a pointer is 4 bytes. None of this is promised by the ISO C standard. It's just the way that nearly all implementations turn out.

Upvotes: 2

Mike
Mike

Reputation: 49363

Typically yes, addresses on 32-bit machines it will be 4 bytes.

Best bet if you don't want to make assumptions is run the old sizeof(p)

Upvotes: 1

Max
Max

Reputation: 2571

Correct. Pointers usually have a fixed size. On a 32-bit machine they are usually 32 bits (= 4 bytes)

Upvotes: 1

Keith Nicholas
Keith Nicholas

Reputation: 44288

yes... it will be 4 bytes... but its not guaranteed.

Upvotes: 2

Related Questions