Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size of a C pointer depends on?

What does the size of a C pointer depend on? Is it the width of the data bus, the width of the address bus, or the word size of the CPU?

I believe it should depend on width of the address bus since a pointer has to address that many locations. Am I right?

(I am looking for a reason for dependency , if any)

like image 393
Lunar Mushrooms Avatar asked Dec 04 '22 03:12

Lunar Mushrooms


2 Answers

The size of a pointer depends on your compiler implementation. In practice it's almost always the native word size of the architecture you're building for.

Notably, the C language standard allows that pointers to different types might have different sizes, the better to support Harvard architectures that have differing sizes of program and data memory, among other things.

like image 168
Carl Norum Avatar answered Dec 31 '22 04:12

Carl Norum


It depends on the CPU architecture in question. You can write portable code that takes this into consideration by using intptr_t (C99 only).

like image 44
Ed S. Avatar answered Dec 31 '22 02:12

Ed S.