指针的大小数据和函数指针之间有什么不同? [英] Can the size of pointers vary between data and function pointers?
本文介绍了指针的大小数据和函数指针之间有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是读指针上中的Ç常见问题的部分。
论述不能够使用无效*
指针持有函数指针,因为数据指针和指向函数可能会对某些平台和<$ C $不同大小C>无效* 只能保证是大到足以容纳指针数据。
谁能给一个平台的例子,数据指针和指向函数实际上有不同的大小?
解决方案
&GT;类型ppp.c
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT全球= 0;诠释主要(无效){
INT本地= 0;
静态INT staticint = 0;
INT *商场;
INT(* FX)(无效); FX =为主;
商城=的malloc(42); / *假设它的工作* /
的printf(#sizeof的指针当地数:%d \\ n,(INT)的sizeof和放大器;地方);
的printf(#sizeof的指针静态数:%d \\ n,(INT)的sizeof和放大器; staticint);
的printf(#sizeof的指针malloc分配数:%d \\ n,(INT)的sizeof商场);
的printf(#sizeof的指针全球数:%d \\ n,(INT)的sizeof和放大器;全局);
的printf(#sizeof的指针的main()数:%d \\ n,(INT)的sizeof FX);
免费(商城);
返回0;
}
&GT; TCC -mc ppp.c
Turbo C的2.01版...
对未使用的变量警告省略...
涡轮增压版本的链接... 2.0
&GT; PPP
#sizeof指向本地:4
#sizeof指针静:4
#sizeof指针malloc分配:4
#sizeof指针全球:4
#sizeof指针到主要():2
&GT; TCC -mm ppp.c
&GT; PPP
#sizeof指向本地:2
#sizeof指针静:2
#sizeof指针malloc分配:2
#sizeof指针全球:2
#sizeof指针到主要():4
TCC -mc
生成的紧凑型的模式code; TCC -mm
生成code。在中等模式
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?
解决方案
> 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
这篇关于指针的大小数据和函数指针之间有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文