C语言 如何在C中动态分配整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40464927/
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
How to dynamically allocate an array of integers in C
提问by H.K
A portion of my C code is shown below.
我的 C 代码的一部分如下所示。
int data[10]={1,3,6,8,1,7,9,1,1,1};
b=10;
int out[b];
process(data, &b, out);
alpha (out, b);
data and out are int arrays. The function process takes the array data whose length is pointed by b (=10) and performs mathematical operation and then returns an array out whose length is then again returned by b (unknown and hence required to be dynamically allocated). Then the array out is sent with function alpha. Right now the function alpha always sends out[10] since b has been declared as 10 in second line of code. How can I allocate array out dynamically so that it contains only valid data returned after function process.
data 和 out 是 int 数组。函数process取b(=10)所指向的数组数据进行数学运算,然后返回一个数组,其长度又由b返回(未知,因此需要动态分配)。然后用函数 alpha 发送数组 out。现在函数 alpha 总是发送 out[10],因为 b 在第二行代码中被声明为 10。如何动态分配数组,使其仅包含函数处理后返回的有效数据。
回答by mousomer
You need to know the difference between dynamic and static allocations.
您需要了解动态分配和静态分配之间的区别。
There are 3 alternatives:
有3种选择:
- Static allocation:
- 静态分配:
You need to know in advance the array length. It must be a number and not a variable:
您需要提前知道数组长度。它必须是数字而不是变量:
int out[10];
Array is staticand is only locally scoped. So if you do:
数组是静态的,仅在本地范围内。所以如果你这样做:
function do_something()
{
int out[10];
}
you can't use the outarray outside the function. But you can define outoutside and send it like this:
您不能在函数外使用out数组。但是你可以在外面定义并像这样发送:
function do_something(int* out)
{
// do things
}
...
{
int out[10];
do_something(out);
}
- Automatic allocation
- 自动分配
When you do
当你做
int b = 100;
int out[b];
(which won't compile on gcc without the -std=c99 or -std=c11 flag), you get an automatic variable, which is very convenient if you don't use outout of scope, but can be a bit dangerous. The resulting array is generated in the Stack, and is destroyed when it goes out of scope (which is why it can get you into trouble if you use it freely). See https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Length.html
(不会不-std = C99或-std = C11标志上的gcc编译),你会得到一个自动变量,如果你不使用,这是非常方便的进行了范围,但可能是有点危险。结果数组在Stack 中生成,当它超出范围时会被销毁(这就是为什么如果你随意使用它会给你带来麻烦)。见 https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Length.html
We suggest you use:
我们建议您使用:
- Dynamic allocation
- 动态分配
Where the array is generated on the Heapand you are responsible to clean it up when you're done with it. The down side is you need to clean it up yourself. The up side is you can use pass it around and use it anywhere.
在堆上生成数组的位置,您有责任在完成后清理它。缺点是你需要自己清理它。好处是你可以使用传递它并在任何地方使用它。
int b=100;
int* out = (int*) malloc(b * sizeof(int));
// do things with out
free(out);
VERY IMPORTANT:Do not change the value of the pointerout. If you do, then you won't free the right amount of memory. A nice thing to do is to copy the pointer, and use the copied address for free:
非常重要:不要改变指针out的值。如果这样做,那么您将无法释放适量的内存。一件好事是复制指针,并免费使用复制的地址:
int b=100;
int* out = (int*) malloc(b * sizeof(int));
int* out_copy = out;
// do things with out. don't touch out_copy
free(out_copy);
回答by Issac Saji
int *out;
out=(int *) malloc(sizeof(int) * 10);
This will produce array outof integertype with size 10.
这将生成大小为 10out的integer类型数组。
回答by molbdnilo
You need outto be a pointer - not an array - and you need to pass a pointer to outto the function, just like you do with b.
你需要out是一个指针——而不是一个数组——并且你需要传递一个指向out函数的指针,就像你对b.
Example:
例子:
void f(int **a, int *size)
{
*a = malloc(23 * sizeof(**a));
*size = 23;
}
/* ... */
int *p = NULL;
int b = 0;
f(&p, &b);
/* 'p' has been allocated and 'b' has its size. */

