gpt4 book ai didi

c - 指针和数组的区别

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:47 27 4
gpt4 key购买 nike

下面有一段代码,它们有什么区别?第一个,结构的buf元素的地址比结构的地址大4,而第二个不是。

首先

#include <stdio.h>

typedef struct A
{
int i;
char buf[]; //Here
}A;

int main()
{
A *pa = malloc(sizeof(A));
char *p = malloc(13);
memcpy(p, "helloworld", 10);
memcpy(pa->buf, p, 13);

printf("%x %x %d %s\n", pa->buf, pa, (char *)pa->buf - (char *)pa, pa->buf);
}

第二

typedef struct A
{
int i;
char *buf; //Here
}A;

最佳答案

第一个是 C99 的“灵活数组成员”。第二个是当您没有 C99 或更高版本时的可靠回退。

使用灵活的数组成员,您可以为数组分配所需的空间以及主要结构:

A *pa = malloc(sizeof(A) + strlen(string) + 1);

pa->i = index;
strcpy(pa->buf, string);

...use pa...

free(pa);

就内存分配而言,buf 成员没有大小(因此 sizeof(A) == sizeof(int) 除非存在填充问题,因为数组对齐——例如,如果你有一个灵活的 double 数组)。

替代方案需要两次分配(和两次发布),或者在设置中需要注意:

typedef struct A2
{
int i;
char *buf;
} A2;

A2 *pa2 = malloc(sizeof(A2));
pa2->buff = strdup(string);

...use pa2...

free(pa2->buff);
free(pa2);

或者:

A2 *pa2 = malloc(sizeof(A2) + strlen(string) + 1);
pa2->buff = (char *)pa2 + sizeof(A2);

...use pa2...

free(pa2);

请注意,使用 A2 需要更多内存,无论是指针的大小(单次分配),还是指针的大小和第二次内存分配的开销(双次分配)。

您有时会看到一些被称为“struct hack”的东西在使用;这早于 C99 标准,并被灵活的数组成员所淘汰。代码如下:

typedef struct A3
{
int i;
char buf[1];
} A3;

A3 *pa3 = malloc(sizeof(A3) + strlen(string) + 1);
strcpy(pa3->buf, string);

这与灵活数组成员几乎相同,但结构更大。在该示例中,在大多数机器上,结构 A3 的长度为 8 个字节(而不是 A 的 4 个字节)。

GCC 对零长度数组有一些支持;您可能会看到数组维度为 0 的 struct hack。这对于任何不模仿 GCC 的编译器都是不可移植的。

它被称为“struct hack”,因为它不能保证语言标准的可移植性(因为您正在声明的数组范围之外访问)。然而,根据经验,它“一直有效”并且可能会继续有效。尽管如此,您应该优先使用灵活的数组成员而不是 struct hack。


ISO/IEC 9899:2011 §6.7.2.1 结构和 union 说明符

¶3 A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

¶18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

关于c - 指针和数组的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804398/

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