c++ - x = malloc(n * sizeof (int));无法将 void 转换为 int

标签 c++ algorithm sorting

我找到了一个计算归并排序执行时间的代码。我收到一条错误消息,提示 “无法将 void 转换为 int”。我从这段代码的部分得到了错误 x = malloc(n * sizeof (int));

    x = malloc(n * sizeof (int));

最佳答案

由于这个问题是用 C++ 标记的,我推荐一个完全不同的解决方案:拥抱 C++ 的强大功能并使用 std::vector

std::vector<int> descriptiveNameHere(n);

这种方法使您不必手动管理内存分配。当允许 vector 超出范围时,存储将自动释放。

Documentation on std::vector

关于c++ - x = malloc(n * sizeof (int));无法将 void 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337761/

相关文章:

c++ - C++ 中的前向声明是什么?

c++ - 在 C++ 中是否不再需要清理代码?

c++ - GCC 内联汇编 : call dword ptr

string - KMP 的失效函数

algorithm - 最大二分匹配方法中的错误

python - NumPy:具有模糊/容忍比较的 np.lexsort

c++ - VS2012 C++ DLL兼容性

c# - 我可以使用什么算法从指定路径开始递归加载整个目录?

javascript - ExtJS 5.0 : Grid remoteSort always sorts by the default 'sorters' only

结构数组的 C++ 选择排序