C语言 在 C/C++ 中初始化大小未知的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19345893/
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
Initializing Arrays in C/C++ With Unknown Size
提问by Johnny
How can I initialize an array in C such as
如何在 C 中初始化数组,例如
void initArr(int size)
{
...
}
The C language does not give the option to initialize an array if his size is not an constant value, and if I initialize it generally (int *arr;) so it gives me error of 'arr' is not being initialized.
如果数组的大小不是常量值,并且我通常对其进行初始化 (int *arr;),则 C 语言不提供初始化数组的选项,因此它给了我 'arr' 未初始化的错误。
Similarily, how can I do that when I have an array with dimension bigger than one (matrix, for example)?
同样,当我有一个维度大于 1 的数组(例如矩阵)时,我该怎么做?
回答by john
The answer that works in C and C++ is dynamic memory allocation
在 C 和 C++ 中有效的答案是动态内存分配
int *arr = (int*)malloc(size*sizeof(int));
In C++ you would prefer to use new instead of malloc, but the principle is the same.
在 C++ 中,您更喜欢使用 new 而不是 malloc,但原理是相同的。
int* arr = new int[size];
回答by haccks
The C language does not give the option to initialize an array if his size is not an constant value
如果数组的大小不是常数值,C 语言不提供初始化数组的选项
In C99 you can use a variable length array and then initialize it by using a loop.
在 C99 中,您可以使用可变长度数组,然后使用循环对其进行初始化。
if I initialize it generally (int *arr;) so it gives me error of 'arr' is not being initialized.
如果我一般初始化它 (int *arr;) 所以它给我'arr'没有被初始化的错误。
This is because a pointer must be initialized (points to a pointee - excluding NULL) before it is being used in program.
这是因为在程序中使用指针之前,必须对其进行初始化(指向被指点者 - 不包括 NULL)。
回答by rici
In C, you can initialize objects to 0 with memset. It's not possible to use memsetto portably initialize objects other than character arrays to a repeated non-zero value.
在 C 中,您可以使用 将对象初始化为 0 memset。无法将memset字符数组以外的对象可移植地初始化为重复的非零值。
In C++, the same is true, but it is restricted to so-called "POD" objects (Plain Old Data), which are basically the same objects you could have in C (no virtual functions, no private data members, etc. -- the precise definition is in the standard). It's not good C++ style, but it's possible.
在 C++ 中,也是如此,但它仅限于所谓的“POD”对象(Plain Old Data),它们与您在 C 中可以拥有的对象基本相同(没有虚拟函数,没有私有数据成员等)- - 准确的定义在标准中)。这不是好的 C++ 风格,但它是可能的。
In both C and C++ you can find the total size of an array in bytes (which you need to pass to memset) by multiplying the dimensions and the size of a single data element. For example:
在 C 和 C++ 中,您可以memset通过将维度和单个数据元素的大小相乘来找到以字节为单位的数组的总大小(您需要传递给)。例如:
void InitializeMatrix(double *m, size_t rows, size_t cols) {
memset(m, 0, rows * cols * sizeof *m);
}
In C99, you can declare a variable length array (VLA), even with multiple dimensions, and if you do so, you can use the sizeofoperator directly on the array, which can be a lot more convenient. But there are lots of restrictions on VLAs; they often don't work the way you expect them to. However, the following does work:
在C99中,你可以声明一个变长数组(VLA),即使是多维的,如果你这样做了,你就可以sizeof直接在数组上使用运算符,这样会方便很多。但是 VLA 有很多限制;它们通常不会像您期望的那样工作。但是,以下确实有效:
double m[rows][cols];
memset(m, 0, sizeof m);
Note that in C99, unlike traditional C or C++, the compiled sizeofoperator in this case may actually create run-time code, and therefore violates the expectation of many programmers that sizeofdoes not evaluate its argument.
请注意,在 C99 中,与传统的 C 或 C++ 不同,sizeof这种情况下的编译运算符实际上可能会创建运行时代码,因此违反了许多sizeof不评估其参数的程序员的期望。

