结构与C中的数组成员

时间:2022-09-06 14:17:38

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? 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?

几乎完全是C中的一个新手,但我并不认为这个结构因使用数组成员而在使用空间方面效率极低?将此结构作为参数传递给函数时会发生什么?它是否完整地复制到堆栈中,包括完整的数组?

Would it be better in most cases to have a double *some_pointer instead?

在大多数情况下,改为使用double * some_pointer会更好吗?

6 个解决方案

#1


7  

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)
{

}

#2


2  

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.

仅取决于程序设计以及如何使用该字段/结构。后者允许使用可变大小的存储,但可能需要动态分配。

#3


1  

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 */

#4


0  

Yes, in C you would usually pass a pointer to the structure around due to efficiency reasons.

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

#5


0  

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.

在结构中使用数组有很多理由。其中之一是结构通过值传递给函数,而数组通过引用传递。也就是说,这个结构可能会传递给带指针的函数。

#6


0  

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; 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.

Offtopic:谨防结构破解,因为它不是严格符合标准的;它忽略了自动填充。然而,Unix IPC消息队列使用它(参见struct msgbuf),几乎可以肯定它可以与任何编译器一起使用。

That said, the functions that use that structure may use pointers to it instead of using a copy.

也就是说,使用该结构的函数可以使用指针而不是使用副本。

#1


7  

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)
{

}

#2


2  

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.

仅取决于程序设计以及如何使用该字段/结构。后者允许使用可变大小的存储,但可能需要动态分配。

#3


1  

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 */

#4


0  

Yes, in C you would usually pass a pointer to the structure around due to efficiency reasons.

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

#5


0  

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.

在结构中使用数组有很多理由。其中之一是结构通过值传递给函数,而数组通过引用传递。也就是说,这个结构可能会传递给带指针的函数。

#6


0  

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; 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.

Offtopic:谨防结构破解,因为它不是严格符合标准的;它忽略了自动填充。然而,Unix IPC消息队列使用它(参见struct msgbuf),几乎可以肯定它可以与任何编译器一起使用。

That said, the functions that use that structure may use pointers to it instead of using a copy.

也就是说,使用该结构的函数可以使用指针而不是使用副本。