什么是保证有关函数指针的大小? [英] What is guaranteed about the size of a function pointer?

查看:97
本文介绍了什么是保证有关函数指针的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我需要知道一个结构,它具有函数指针在它的大小。我可以保证在所有平台和架构:

In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures:


  • 一个void *的大小尺寸作为函数指针一样吗?

  • 函数指针的大小没有什么不同,因为它的返回类型?

  • 函数指针的大小不会因为它的参数类型不同?

我认为答案是肯定的所有这些,但我想是肯定的。对于情况下,我打电话的sizeof(结构MYSTRUCT),仅此而已。

I assume the answer is yes to all of these, but I want to be sure. For context, I'm calling sizeof(struct mystruct) and nothing more.

推荐答案

从C99规范,第6.2.5节,第27段:

From C99 spec, section 6.2.5, paragraph 27:

一个指向void者具有相同
  再presentation和对齐
  要求的一个指针
  字符类型。同样,指针
  到合格网络版或unquali网络版版本
  兼容类型应具有相同的
  再presentation和对齐
  要求。所有指针
  结构类型应具有相同的
  再presentation和对齐
  要求对方。所有
  指向联合类型应有
  同样重presentation和对齐
  要求对方。指针
  其它类型的不必具有相同的
  再presentation或对齐
  要求。

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

因此​​,没有;不能保证一个无效* 可容纳一个函数指针。

So no; no guarantee that a void * can hold a function pointer.

和6.3.2.3节,第8段:

And section 6.3.2.3, paragraph 8:

一个指向包含一个类型的函数
  可被转化为一个指针
  另一种类型和背部的功能
  再次;结果应该比较平等
  到原来的指针。

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer.

这意味着一个函数指针类型可以持有任何其他函数指针的值。从技术上讲,这是不一样的低保函数指针类型的大小不会改变,只是说他们的价值观占据同一范围内的对方。

implying that one function pointer type can hold any other function pointer value. Technically, that's not the same as guaranteeing that function-pointer types can't vary in size, merely that their values occupy the same range as each other.

这篇关于什么是保证有关函数指针的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆