【问题标题】:How to declare the size of an array at runtime in C?如何在 C 中在运行时声明数组的大小?
【发布时间】:2009-03-15 20:56:39
【问题描述】:

我基本上想要相当于这个的C(嗯,只是数组的部分,我不需要类和字符串解析等等):

public class Example
{
    static int[] foo;
    public static void main(String[] args)
    {
        int size = Integer.parseInt(args[0]);
        foo = new int[size]; // This part
    }
}

请原谅我的 C 无知。我已经被java破坏了;)

【问题讨论】:

  • 看起来你也被 C++ 破坏了 ;)

标签: c arrays


【解决方案1】:
/* We include the following to get the prototypes for:
 * malloc -- allocates memory on the freestore
 * free   -- releases memory allocated via above
 * atoi   -- convert a C-style string to an integer
 * strtoul -- is strongly suggested though as a replacement
*/
#include <stdlib.h>
static int *foo;
int main(int argc, char *argv[]) {
    size_t size = atoi(argv[ 1 ]); /*argv[ 0 ] is the executable's name */
    foo = malloc(size * sizeof *foo); /* create an array of size `size` */
    if (foo) {  /* allocation succeeded */
      /* do something with foo */
      free(foo); /* release the memory */
    }
    return 0;
}

警告:现成的东西,没有任何错误检查。

【讨论】:

  • 你应该提到“if (foo) free(foo);”是“垃圾收集”。
  • C 中没有垃圾收集。我会撒谎 ;-) 但我确实放入了一些 cmets。
  • 我认为 foo 不应该被声明为静态的,与 java 的含义不同。这里我们只需要一个全局变量。
  • 静态有什么问题?它是全局的(文件范围),具有内部链接,恕我直言,OP 比我编写更智能的版本(没有全局变量)更容易理解。此外,这是一个现成的版本,还有很多不足之处。
  • @dirkgently 你的回答很好。我只是说将 foo 声明为 int * 就足够了。在 java 中,需要静态声明一个变量,该变量只会被实例化一次,而不是每个新对象一次。
【解决方案2】:

在 C 中,如果你忽略错误检查,你可以这样做:

#include <stdlib.h>
static int *foo;

int main(int argc, char **argv)
{
     int size = atoi(argv[1]);
     foo = malloc(size * sizeof(*foo));
     ...
}

如果你不想要一个全局变量并且你使用的是 C99,你可以这样做:

int main(int argc, char **argv)
{
    int size = atoi(argv[1]);
    int foo[size];
    ...
}

这使用 VLA - 可变长度数组。

【讨论】:

    【解决方案3】:

    不幸的是,这个问题的许多答案,包括已接受的答案,都是正确,但不等同于 OP 的代码 sn-p。请记住,operator new[] 为每个数组元素调用默认构造函数。对于像 int 这样没有构造函数的 POD 类型,它们是默认初始化的(阅读:零初始化,参见 The C++ Standard 的 §8.5 ¶5-7)。

    我刚刚将malloc(分配未初始化的内存)换成了calloc(分配零内存),所以相当于给定的C++ sn-p 将是

    #include <stdlib.h>  /* atoi, calloc, free */
    
    int main(int argc, char *argv[]) {
        size_t size = atoi(argv[1]);
        int *foo;
    
        /* allocate zeroed(!) memory for our array */
        foo = calloc(sizeof(*foo), size);
        if (foo) {
            /* do something with foo */
    
            free(foo); /* release the memory */
        }
    
        return 0;
    }
    

    很抱歉重新提出这个老问题,但不发表评论就离开感觉不对(我没有所需的代表);-)

    【讨论】:

      【解决方案4】:

      如果需要初始化数据,可以使用calloc:

      int* arr = calloc (nb_elems, sizeof(int));
      /* Do something with your array, then don't forget to release the memory */
      free (arr);
      

      这样,分配的内存将被初始化为零,这很有用。请注意,您可以使用任何数据类型来代替 int。

      【讨论】:

        【解决方案5】:
        int count = getHowManyINeed();
        int *foo = malloc(count * sizeof(int));
        

        【讨论】:

          猜你喜欢
          • 2014-07-06
          • 2012-07-14
          • 1970-01-01
          • 2012-03-12
          • 1970-01-01
          • 2019-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多