- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已阅读 this post 。
从那里,我读到:
来自 C99:6.2.7.27
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) 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. (emphasis mine)
我对这个问题的目的对我来说很重要的部分的解释似乎是说
如果我有:
int *a, **b;
保证注册和对齐,并且所有这些陈述都是真实的;
sizeof(a)==sizeof(*a)&&
sizeof(int *)==sizeof(b)&&
sizeof(*b)==sizeof(**b);// all essentially int pointers,
// and would be equal
但如果我有:
int *a;
float*b;
注册和对齐不得到保证。即:
sizeof(a)!=sizeof(b)&&
sizeof(float *)!=sizeof(int *)&&
sizeof(*b)!=sizeof(*a);//all pointers, but not of compatible types
//therefore not guaranteed to be equal.
我问的原因是因为 this 讨论,
我在其中发布了一个显示创建 3D 数组的函数的答案:
int *** Create3D(int p, int c, int r)
{
int ***arr;
int x,y;
arr = calloc(p, sizeof(arr));
for(x = 0; x < p; x++)
{
arr[x] = calloc(c ,sizeof(arr));
for(y = 0; y < c; y++)
{
arr[x][y] = calloc(r, sizeof(int));
}
}
return arr;
}
就使用 sizeof() 而言,以下语句是否安全?
arr = calloc(p, sizeof(arr));
或者,即使只使用了 int
类型,它应该是:
arr = calloc(p, sizeof(int **));
或:
arr = calloc(p, sizeof *arr);
问题:
给定 arr
声明为 int ***
:
对于分配内存,只要类型保持 int
是否存在使用任何 int 指针变体的危险 (int *, int **, arr, *arr, int ***
) 作为 sizeof 的参数?
一种形式优于另一种形式吗? (请给出风格以外的原因)
最佳答案
My interpretation of the parts important to me for the purpose of this question seem to say if I have:
int *a, **b;
registration and alignment are guaranteed,
a
是指向 int
的指针。 b
是指向int *
的指针。这些是不兼容的类型,标准不要求指向这些类型的指针具有相同的表示或对齐方式。
and that all of these statements are true[:]
sizeof(a)==sizeof(*a)&&
sizeof(int *)==sizeof(b)&&
sizeof(*b)==sizeof(**b);// all essentially int pointers,
// and would be equal
不,该标准不要求任何为真。第一个在 64 位系统上经常是错误的。其他的通常是正确的,但如果您正在寻找保证,那么标准不会提供它们。
but if I have:
int *a;
float*b;registration and alignment are not guaranteed. i.e.:
正确,float *
和int *
的表示和对齐不保证相同。
The reason I ask is because of this discussion, where I posted an answer showing a function that creates a 3D array: [..] int ***arr; int x,y;
arr = calloc(p, sizeof(arr));
这通常会按预期工作,因为在大多数系统上,所有对象指针实际上都具有相同的大小,但 C 并不要求它是正确的。应该是:
arr = calloc(p, sizeof(*arr));
或
arr = calloc(p, sizeof(int **));
同样,这个:
arr[x] = calloc(c ,sizeof(arr));
应该是
arr[x] = calloc(c ,sizeof(*arr[x]));
或
arr[x] = calloc(c ,sizeof(int *));
.不过这个没问题:
arr[x][y] = calloc(r, sizeof(int));
For allocating memory, and as long as type stays int, in that statement, is there any danger of using any variation of int pointer (int *, int **, arr, *arr) as the argument to sizeof ?
是的。给定任何类型 T
,T *
是不同的、不兼容的类型。无论资格或指针如何,该标准都不保证两者具有相同的表示或对齐方式。不能保证相同表示的部分原因是不能保证相同大小。特别是,如果 T
是 int
,那么通常情况下 T
和 T *
的大小不同。
Is one form preferred over the other? (please give reasons other than style)
虽然这是否是一个风格点是有争议的,但这种形式:
arr = calloc(p, sizeof(*arr));
的优点是如果你改变arr
的类型,你不必修改calloc
调用。正确的大小来自 arr
的声明。也很容易判断大小是否正确,无需查找 arr
的声明。如果您愿意,可以很容易地围绕该表单编写宏。
关于是否可以为家族中的合格类型假定表示和对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32896271/
关于 Stylus 的一大优点是它允许您定义可用于自定义结果输出配置的变量。 例如, // my-html-object.styl $my-html-object-color = red $my-ht
我是 C++ 的新手,只有一个 C++ 的小头文件,里面有一个简单的结构。 PGNFinder.h: #ifndef PGNFINDER_H #define PGNFINDER_H struct Fi
我正在为 Windows 编写一段 C++ 代码,需要查询进程及其每个单独线程的计时。 为了进行必要的系统调用,我需要进程及其每个线程的句柄。我正在使用 getCurrentProcess 和 get
我正在尝试从数据库中检索数据,但将其限制为每个 View 的特定数量的项目。但相反,我得到了上述错误。我创建了以下函数来检索数据: //function to display jobs functio
我有一个用于我的 C++ 应用程序的 settings.ini 文件,但是我的一个用户 Windows\Temp 坏了,导致它对他来说一团糟。所以我打算将它移到他的用户文件夹中。所以我想在全局变量部分
This post says如果您的数据大于 2GB,您需要 64 位系统用于 MongoDB。 在我的本地机器上,运行 32 位 Mac OS X Leopard 的 64 位 Intel C2D,
Thymeleaf tutorial code 这个 SeedStarter 是如何实例化的?请参阅方法主体显示与对象 SeedStarter 的交互。那么它首先是如何实例化的? 由于这个项目是一个教
因此,在这个相当大的源文件中,我有以下部分,假设在函数 foo 中,从 main 调用: FILE *logfile = NULL if (log_engabled) { char fname
出于某种原因,我坚持使用 List 作为集合类型的模型类,我想在客户端使用该模型。然而,GWT 当然无法序列化 java.util.List。但是,此模型中 List 的所有实现都基于 ArrayLi
如何使用 JavaScript 解析此 CSV? 1363085391,42.890000000000,5.432200000000 1363088879,47.570000000000,4.9818
我正在使用名为 paypal_class 的 codeigniter 库。一切都很好,但是在验证 ipn 函数时我得到了这个错误。 使用未定义的常量主机 - 假定为“主机”,这一定是邮件未发送给用户的
这个问题已经有答案了: Forcing GCC to compile .cpp file as C (2 个回答) 已关闭 10 年前。 我有一个 C++ 项目 [IDE = codelite],它尝
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 6 年前。 我创建了一个 vector vector ,我想根据我定义的参
我是一名优秀的程序员,十分优秀!