如何在C中动态分配整数数组 [英] How to dynamically allocate an array of integers in C

查看:305
本文介绍了如何在C中动态分配整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C代码的一部分如下所示.

A portion of my C code is shown below.

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和out是int数组.函数过程获取长度为b(= 10)的数组数据并执行数学运算,然后返回其长度再由b返回的数组(未知,因此需要动态分配).然后,将数组输出与函数alpha一起发送.现在,由于在第二行代码中b被声明为10,所以函数alpha总是发出out [10].如何动态分配数组,使其仅包含函数处理后返回的有效数据.

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.

推荐答案

您需要了解动态分配和静态分配之间的区别.

You need to know the difference between dynamic and static allocations.

有3种选择:

  • 静态分配:

您需要提前知道数组长度.它必须是数字,而不是变量:

You need to know in advance the array length. It must be a number and not a variable:

int out[10];

数组为静态,并且仅在本地范围内.因此,如果您这样做:

Array is static and is only locally scoped. So if you do:

function do_something()
{
   int out[10];
}

您不能在函数外部使用 out 数组.但是您可以定义 out 并像这样发送:

you can't use the out array outside the function. But you can define out outside and send it like this:

function do_something(int* out)
{
   // do things
}
...
{
   int out[10];
   do_something(out);
}

  • 自动分配
    • Automatic allocation
    • 完成时

      int b = 100;
      int out[b];
      

      (如果没有-std = c99或-std = c11标志,它将无法在gcc上编译),您会得到一个自动变量,如果您不使用 out out,这将非常方便范围,但可能有点危险.生成的数组在 Stack 中生成,并在超出范围时被销毁(这就是为什么如果您随意使用它会给您带来麻烦的原因).看 https://gcc.gnu.org/onlinedocs/gcc -5.1.0/gcc/Variable-Length.html

      (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 out out 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

      我们建议您使用:

      • 动态分配

      上生成数组的位置,完成后您有责任清理它.缺点是您需要自己清理.有利的一面是,您可以使用它来回传递,并可以在任何地方使用它.

      Where the array is generated on the Heap and 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);
      

      非常重要: 请勿更改指针 out 的值.如果这样做,则将无法释放适量的内存.一件不错的事情是复制指针,并免费使用复制的地址:

      VERY IMPORTANT: Do not change the value of the pointer out. 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:

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

      这篇关于如何在C中动态分配整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆