C语言 在 C 中,如何创建具有零值但没有 calloc 的常量大小的“静态”数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18283688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C, how do I create a `static` array of constant size with zeroed values, but without calloc?
提问by Michael Bishop
I currently have some code in a function that looks like this:
我目前在一个函数中有一些代码,如下所示:
static const int kFrameCountSample = 250;
static float * samples = (float *)calloc(kFrameCountSample, sizeof(float));
I like that the samples array is zeroed out exactly once with calloc().
我喜欢用calloc().
I can also write the code so samplesis allocated on the stack.
我也可以编写代码,以便samples在堆栈上分配。
static const int kFrameCountSample = 250;
static float samples[kFrameCountSample];
but now samplesis not initialized to zero values. How would I also initialize it at the time it is allocated?
但现在samples没有初始化为零值。我如何在分配时对其进行初始化?
回答by Jacob Pollack
For completeness (note: this is C99 NOT C++):
为了完整性(注意:这是 C99 而不是 C++):
It's important to note that if you define and initialize a static array of length kto less than k - 1values then the rest will be zero-filled. Hence:
重要的是要注意,如果您定义并初始化一个长度k小于k - 1值的静态数组,则其余部分将被零填充。因此:
static float samples[kFrameCountSample];
... is identical to:
... 等同于:
static float samples[kFrameCountSample] = { 0 }; // Zero-fills.
... and will zero-fill samples. Moreover, if you do the following:
... 并将零填充samples。此外,如果您执行以下操作:
static float samples[kFrameCountSample] = { 1, 2, 3 }; // Zero-fills elements of position 3 ... 250.
... it will zero-fill the rest of the elements that are not assigned in the initialization of samples.
...它将零填充在 的初始化中未分配的其余元素samples。
Remark:
评论:
- Global variables are automatically zero-filled (and do not need to be initialized if this is the intent).
- The standard for uninitialized static objects in section 6.7.8.10of the C99 standard says:
- 全局变量自动填充零(如果这是意图,则不需要初始化)。
- C99 标准第6.7.8.10节中未初始化静态对象的标准说:
"If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
“如果没有明确初始化具有自动存储期的对象,则其值是不确定的。如果没有明确初始化具有静态存储期的对象,则:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules."
- 如果是指针类型,则初始化为空指针;
- 如果它有算术类型,则初始化为(正或无符号)零;
- 如果是聚合,则根据这些规则(递归地)初始化每个成员;
- 如果是联合,则根据这些规则(递归地)初始化第一个命名成员。”
回答by caf
The code that you've given:
您提供的代码:
static const int kFrameCountSample = 250;
static float samples[kFrameCountSample];
is not valid C. Objects with static storage duration can't have variably-modified type. On the other hand, this would be valid:
无效 C. 具有静态存储期的对象不能具有可变修改类型。另一方面,这将是有效的:
#define kFrameCountSample 250
static float samples[kFrameCountSample];
This is valid either at file scope or at function scope. In both cases the array sampleshas static storage duration, which means that it exists for the entire life of the program and is initialized only once, at program startup. The onlydifference is that if it's in a function, the scope of the name samplesis restricted to the block in which it's declared. In neither case is it likely to be allocated on the stack.
这在文件范围或函数范围内都有效。在这两种情况下,数组samples都有静态存储持续时间,这意味着它在程序的整个生命周期中都存在,并且只在程序启动时初始化一次。该唯一的区别是,如果它在一个函数,这个名字的范围samples被限制在它的声明块。在这两种情况下,它都不太可能在堆栈上分配。
The array sampleshere iszero-initialized - objects with static storage duration are never uninitialized. If you do not provide an explicit initializer, they are initialized to zero of the appropriate type.
samples这里的数组是零初始化的 -具有静态存储持续时间的对象永远不会未初始化。如果您不提供显式初始化程序,它们将被初始化为适当类型的零。
If you dowant an array stored on the stack - that is, created when the function containing the declaration is entered and destroyed when that function is exited, what C calls automatic storage duration- then you must declare it inside a function, omit the statickeyword and add an explicit initializer:
如果您确实希望将数组存储在堆栈中 - 即在包含声明的函数进入时创建并在该函数退出时销毁,C 称为自动存储持续时间- 那么您必须在函数内声明它,省略static关键字并添加一个显式初始化程序:
float samples[kFrameCountSample] = { 0 };
(In this case kFrameCountSampledoes not need to be a macro, it can be a static const intif you want).
(在这种情况下kFrameCountSample不需要是一个宏,static const int如果你愿意,它可以是一个)。
A single { 0 }is fine no matter what size the array is, because objects in C are never partially initialized - if you initialize any sub-object of an array or structure, the remaining sub-objects are initialized to zero of the appropriate type, just as with objects of static storage duration.
{ 0 }无论数组的大小如何,单个都可以,因为 C 中的对象永远不会部分初始化 - 如果初始化数组或结构的任何子对象,则其余子对象将初始化为适当类型的零,就像具有静态存储持续时间的对象。
回答by TheExplosiveSheep
An array that only has some positions initialized to a value will have zeroes at the remaining positions.
只有一些位置初始化为值的数组将在其余位置为零。
static const int kFrameCOuntSample = 250;
static float samples[kFrameCOuntSample] = { 0 };
Should do the trick.
应该做的伎俩。
回答by Ion
You can do memset to avoid any chances of something not being initlaized to zero.
您可以执行 memset 以避免任何未初始化为零的机会。
static float samples[kFrameCountSample];
memset(samples,0,kFrameCountSample*sizeof(float));
http://man7.org/linux/man-pages/man3/memset.3.html
http://man7.org/linux/man-pages/man3/memset.3.html
What it does is set a to every byte in samples. Meaning it's going to be initialized quickly to 0.
它所做的是为样本中的每个字节设置一个。这意味着它将被快速初始化为 0。

