How can I find the size of the type "pointer?" For example, if I want to know the size of an integer, I can use "sizeof(int)". What is the equivalent for a pointer? Is it possible to do this without initializing the pointer?
3 Answers
sizeof(void*)
generally yields the size of pointers on your architecture, although there may be architectures where pointers to specific types are smaller - since void *
is guaranteed to allow a full round-trip to any data pointer (C99 §6.3.2.3 ¶1), it should be surely bigger or equal to all other data pointers.
Pointers to functions are a whole different beast, which may require more space than a plain void *
(think microcontrollers, where code and data may reside in completely different address spaces). Here you can get an idea of the situation with something like sizeof(void (*)())
, although IIRC nothing stops a particular implementation to have different sizes for each and every function pointer type actually, C99 §6.3.2.3 ¶8 requires that a function pointer must survive a roundtrip through any other function pointer type, so I guess it's safe to use any function pointer type to probe function pointers size in the same way as void *
is a good measure for data pointers size.

- 123,740
- 17
- 206
- 299
-
Are you sure about that? If a function pointer would have a different size and format then how would your call instruction look like, and how would the virtual address lookup for the call be handled? This is why micro-controllers communicate through MMIO and interrupts, and not function calls. – Jens Jun 22 '15 at 01:47
-
@Jens: The compiler would generate code to load the "large address" from data memory (maybe with some magic instruction that reads code pointers, maybe loading it in two separate small registers) and do the call, I don't see any problems with it, almost any Harvard architecture has to deal with this... Interestingly, this fact was exploited even on 16-bit x86 (which is a Von Neumann architecture) in times where bizarre memory models flourished. For more detailed answers (with examples), see http://stackoverflow.com/q/12358843/214671. – Matteo Italia Jun 22 '15 at 07:33
-
I agree for low-level, device dependent, even architecturally hybrid code that is outside of today's userspace code. Though I doubt that's what the OP's aiming for :) Thanks for the link! – Jens Jun 22 '15 at 15:22
-
@Jens: of course, current "normal" architectures have a single pointer size (and it's a feature commonly relied on in many APIs - Win32 comes to mind), it's just a minor point that it's interesting to know :-) . Besides, on bizarro architectures usually this provision alone is not enough to work efficiently (see the `far` and `near` pointers on x86, the `progmem` qualifier on pointer on AVR and the like), so it's not such a big deal. – Matteo Italia Jun 22 '15 at 15:41
Try using a plain, untyped void pointer like so:
size_t sz = sizeof(void *);
Just be aware that the size of a pointer (like the size of the integer) is not the same for all architectures that your program may be compiled for.

- 8,423
- 9
- 58
- 78
-
`sizeof` returns data in type `size_t`, which isn't the same as `unsigned int` on some platforms – PC Luddite Jun 22 '15 at 04:08
"pointer" isn't a type. It can be part of a type. "Pointer to int" would be an example of a type, and you can find the size of that by writing sizeof(int *)
.
Different types of pointer may have different sizes, although on modern hardware they are usually all the same.

- 138,810
- 21
- 208
- 365
-
-
@ThePcLuddite the question doesn't make sense. He asks "how can I find the size of the type 'pointer'", however there is no such type. – M.M Jul 10 '15 at 12:41