gpt4 book ai didi

c - 数组与 malloc?

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:49 25 4
gpt4 key购买 nike

如果您想要一个int 大小 的动态分配数组,有两种方法可以在C 中获得:

int* array = new int [size]; //method 1
int* array2 = malloc (sizeof(int) * size); //method 2

什么时候应该使用每种方法?

最佳答案

如果你在 C 中,则为第二个。

如果您使用的是 C++,请使用 std::vector<int>如果可以的话;否则使用第一个(很少需要)。

关于c - 数组与 malloc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19748448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com