gpt4 book ai didi

是否可以为家族中的合格类型假定表示和对齐

转载 作者:行者123 更新时间:2023-12-02 08:25:08 25 4
gpt4 key购买 nike

我已阅读 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 ?

是的。给定任何类型 TT * 是不同的、不兼容的类型。无论资格或指针如何,该标准都不保证两者具有相同的表示或对齐方式。不能保证相同表示的部分原因是不能保证相同大小。特别是,如果 Tint,那么通常情况下 TT * 的大小不同。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com