Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the size of pointers vary between data and function pointers?

Tags:

I was just reading the section of the C FAQ on pointers.

It discusses not being able to use void * pointers to hold function pointers because pointers to data and pointers to functions may have differing sizes on some platforms and void * is only guaranteed be large enough to hold pointers to data.

Can anyone give an example of a platform where pointers to data and pointers to functions actually have differing sizes?

like image 418
Robert S. Barnes Avatar asked Sep 24 '09 20:09

Robert S. Barnes


People also ask

Are pointers all the same size?

Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine.

Does size of pointer depend on datatype?

It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed.

What is the size of a function pointer?

A function-pointer is a 4-byte elementary item . Function-pointers have the same capabilities as procedure-pointers, but are 4 bytes in length instead of 8 bytes. Function-pointers are thus more easily interoperable with C function pointers.

Is pointer size constant?

Function Pointers can have very different sizes, from 4 to 20 bytes on an x86 machine, depending on the compiler. So the answer is no - sizes can vary.


2 Answers

> type ppp.c #include <stdio.h> #include <stdlib.h>  int global = 0;  int main(void) {     int local = 0;     static int staticint = 0;     int *mall;     int (*fx)(void);      fx = main;     mall = malloc(42); /* assume it worked */     printf("#sizeof pointer to local: %d\n", (int)sizeof &local);     printf("#sizeof pointer to static: %d\n", (int)sizeof &staticint);     printf("#sizeof pointer to malloc'd: %d\n", (int)sizeof mall);     printf("#sizeof pointer to global: %d\n", (int)sizeof &global);     printf("#sizeof pointer to main(): %d\n", (int)sizeof fx);     free(mall);     return 0; } > tcc -mc ppp.c Turbo C  Version 2.01 ... warnings about unused variables elided ... Turbo Link  Version 2.0 ... > ppp #sizeof pointer to local: 4 #sizeof pointer to static: 4 #sizeof pointer to malloc'd: 4 #sizeof pointer to global: 4 #sizeof pointer to main(): 2 > tcc -mm ppp.c > ppp #sizeof pointer to local: 2 #sizeof pointer to static: 2 #sizeof pointer to malloc'd: 2 #sizeof pointer to global: 2 #sizeof pointer to main(): 4 

tcc -mc generates code in the "compact" model; tcc -mm generates code in the "medium" model

like image 64
pmg Avatar answered Oct 09 '22 21:10

pmg


On real-mode x86, code & data is accessed by segment + offset, each a 16-bit quantity. "Near" pointers were 16-bit only and used the current segment, "Far" pointers were 32-bit and specified the segment and offset. For C compilers, there were several different memory models you could choose, with different defaults of near or far pointers for code and data.

For example, the "Medium" memory model used near pointers for data but far pointers for code by default.

I wouldn't be surprised if some modern embedded processors have similar memory models.

like image 22
Michael Avatar answered Oct 09 '22 21:10

Michael