初始化c中的struct数组

时间:2022-10-11 21:26:11

I should store some info in a vector of struct that comes in a little at a time. I know in advance the size of the array. My question is: should I initialize the array with structs that for me represent an invalid input? for example should I do the following:

我应该将一些信息存储在一次一点点的结构向量中。我事先知道数组的大小。我的问题是:我应该使用对我来说代表无效输入的结构来初始化数组吗?例如,我应该做以下事情:

typedef struct mystruct{
    int ID;
    int xvalue;
} my_struct;

#define NO_INPUT (my_struct) { ID=-1, xvalue=0}

where ID=-1 is an input that doesn't make sense for me. After the definition should I initialize the array to NO_INPUT? What is the best practice? PS Is the #define directive right? Is it compatible with C89 standards? Thanks in advance!

其中ID = -1是对我没有意义的输入。定义之后我应该将数组初始化为NO_INPUT吗?什么是最佳做法? PS #define指令是对的吗?它与C89标准兼容吗?提前致谢!

1 个解决方案

#1


3  

Choosing to initialize storage when it's not really necessary has costs and advantages. C lets you pick your poison.

选择在不真正需要时初始化存储具有成本和优势。 C让你挑选毒药。

The cost is touching every allocated byte. For big arrays, this may be expensive. It can hurt CPU cache performance. Some C runtimes don't map malloced pages into a program's address space until they're touched. Initializing them defeats this optimization.

成本接触每个分配的字节。对于大型阵列,这可能很昂贵。它可能会损害CPU缓存性能。某些C运行时不会将malloced页面映射到程序的地址空间,直到它们被触摸为止。初始化它们会使这种优化失败。

The advantage is in mitigating and fixing bugs. Derefing a pointer initialized to NULL causes an immediate seg fault for many modern OS/CPUs. This kind of "fail fast" behavior is a good thing. Uninitialized pointers can point to real data, so the buggy code keeps running, producing bad answers or failing much later. Initialized values also make more sense in debuggers and error messages. They also make it possible to add assertions that a location hasn't been used since initialization in order to detect unintended overwrites.

优点是减轻和修复错误。将初始化为NULL的指针解析为许多现代OS / CPU会立即出现seg错误。这种“快速失败”的行为是一件好事。未初始化的指针可以指向实际数据,因此错误的代码会继续运行,产生错误的答案或者很快失败。初始化值在调试器和错误消息中也更有意义。它们还可以添加自初始化以来尚未使用位置的断言,以便检测意外的覆盖。

Due to the advantages, even when the cost of initialization is too high, some programs are designed with a "debugging mode" that either includes extra initialization and checking code with preprocessor directives or enables/disables them dynamically at run time.

由于这些优点,即使初始化成本太高,一些程序也设计有“调试模式”,包括额外的初始化和使用预处理器指令检查代码,或者在运行时动态启用/禁用它们。

#1


3  

Choosing to initialize storage when it's not really necessary has costs and advantages. C lets you pick your poison.

选择在不真正需要时初始化存储具有成本和优势。 C让你挑选毒药。

The cost is touching every allocated byte. For big arrays, this may be expensive. It can hurt CPU cache performance. Some C runtimes don't map malloced pages into a program's address space until they're touched. Initializing them defeats this optimization.

成本接触每个分配的字节。对于大型阵列,这可能很昂贵。它可能会损害CPU缓存性能。某些C运行时不会将malloced页面映射到程序的地址空间,直到它们被触摸为止。初始化它们会使这种优化失败。

The advantage is in mitigating and fixing bugs. Derefing a pointer initialized to NULL causes an immediate seg fault for many modern OS/CPUs. This kind of "fail fast" behavior is a good thing. Uninitialized pointers can point to real data, so the buggy code keeps running, producing bad answers or failing much later. Initialized values also make more sense in debuggers and error messages. They also make it possible to add assertions that a location hasn't been used since initialization in order to detect unintended overwrites.

优点是减轻和修复错误。将初始化为NULL的指针解析为许多现代OS / CPU会立即出现seg错误。这种“快速失败”的行为是一件好事。未初始化的指针可以指向实际数据,因此错误的代码会继续运行,产生错误的答案或者很快失败。初始化值在调试器和错误消息中也更有意义。它们还可以添加自初始化以来尚未使用位置的断言,以便检测意外的覆盖。

Due to the advantages, even when the cost of initialization is too high, some programs are designed with a "debugging mode" that either includes extra initialization and checking code with preprocessor directives or enables/disables them dynamically at run time.

由于这些优点,即使初始化成本太高,一些程序也设计有“调试模式”,包括额外的初始化和使用预处理器指令检查代码,或者在运行时动态启用/禁用它们。