简体   繁体   English

结构与C中的数组成员

[英]Struct with array member in C

Recently I reviewed some C code and found something equivalent to the following: 最近我查看了一些C代码,发现了类似于以下内容:

struct foo {
    int some_innocent_variables;
    double some_big_array[VERY_LARGE_NUMBER];
}

Being almost, but not quite, almost entirely a newbie in C, am I right in thinking that this struct is awfully inefficient in its use of space because of the array member? 几乎完全是C中的一个新手,我几乎完全是一个新手,我是否正确地认为这个结构因使用数组成员而在使用空间方面效率极低? What happens when this struct gets passed as an argument to a function? 将此结构作为参数传递给函数时会发生什么? Is it copied in its entirety on the stack, including the full array? 它是否完整地复制到堆栈中,包括完整的数组?

Would it be better in most cases to have a double *some_pointer instead? 在大多数情况下,改为使用double *some_pointer会更好吗?

If you pass by value yes it will make a copy of everything. 如果你通过值是,它将复制一切。 But that's why pointers exist. 但这就是指针存在的原因。

//Just the address is passed 
void doSomething(struct foo *myFoo)
{

}

Being passed as an argument it will be copied which is very inefficient way of passing structures, especially big ones. 作为参数传递它将被复制,这是传递结构,特别是大结构的非常低效的方式。 However, basically, structs are passed to functions by pointer. 但是,基本上,结构通过指针传递给函数。

Choosing between 选择之间

double some_big_array[VERY_LARGE_NUMBER];

and

double *some_pointer

depends only on the program design and how this field/structure will be used. 仅取决于程序设计以及如何使用该字段/结构。 The latter allows using variable size storage, however may need dynamic allocation. 后者允许使用可变大小的存储,但是可能需要动态分配。

As others have said, objects of that type are usually passed around with pointers (always sizeof (struct foo) bytes, often 4 bytes). 正如其他人所说,这种类型的对象通常用指针传递(总是sizeof (struct foo)字节,通常是4个字节)。

You may also see the "struct hack" (also passed around with pointers): 您可能还会看到“struct hack”(也传递了指针):

struct foo {
    int some_innocent_variables;
    double some_array[]; /* C99 flexible array member */
    /* double some_array[1]; ** the real C89 "struck hack" */
}

This "struct hack" gets an array sized by the malloc call. 这个“struct hack”获取一个由malloc调用调整大小的数组。

/* allocate an object of struct foo type with an array with 42 elements */
struct foo *myfoo = malloc(sizeof *myfoo + 42 * sizeof *myfoo->some_array);
/* some memory may be wasted when using C89 and
   the "struct hack" and this allocation method */

是的,在C中,由于效率原因,您通常会将指针传递给周围的结构。

There are plenty of reasons to use arrays in structs. 在结构中使用数组有很多理由。 Among them is the fact that structs are passed to functions by value, while arrays are passed by reference. 其中之一是结构通过值传递给函数,而数组通过引用传递。 That said, this struct is probably passed to functions with pointers. 也就是说,这个结构可能会传递给带指针的函数。

EDIT: That structure is fine as long as you pass it by reference (using a pointer). 编辑:只要您通过引用传递它(使用指针),该结构就可以了。

Offtopic: Beware of the struct hack, as it is not strictly standard compliant ; Offtopic:谨防结构破解,因为它不严格符合标准 ; it ignores the automatic padding. 它忽略了自动填充。 The Unix IPC messaging queues use it (see struct msgbuf ), though, and it is almost certainly to work with any compiler. 然而,Unix IPC消息队列使用它(参见struct msgbuf ),几乎可以肯定它可以与任何编译器一起使用。

That said, the functions that use that structure may use pointers to it instead of using a copy. 也就是说,使用该结构的函数可以使用指针而不是使用副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM