gpt4 book ai didi

c++ - 已分配但未初始化

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:00 26 4
gpt4 key购买 nike

我遇到了一个已分配但未初始化的问题。

部分代码如下:

void test2(vector<string> names, int num) // just for test
{
const char **tmp = new const char *[num]; // issue happends here.
for(int i = 0; i < num; i ++)
tmp[i] = names[i].c_str(); // is it not inialization???
//call another function using tmp
delete []tmp;
}

在代码的第 3 行,我遇到了一个问题:Assigning: "tmp"= "new char const *[num]", which is allocated but not initialized.

我相信我对二维数组分配和初始化感到困惑。我认为 tmp 是 const char * 数组,我只想将 vertor 转换为 const char **;

那么在代码中,这里用new和delete对吗?

我知道如果二维数组是 int*,那么如果我想给它赋值,我需要 new int[num],然后对 new int[] 做一个 for 循环;但是我这里的情况呢?

有人可以帮我处理这段代码吗?

最佳答案

在这种情况下不要使用 const,因为您是在初始化后分配数据。

关于c++ - 已分配但未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16529324/

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