已分配但未初始化
allocated but not initialized
我遇到了一个已分配但未初始化的问题。
下面是部分代码:
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行,我遇到了一个问题:赋值:"tmp" = "新char const *[num]",这是分配的,但没有初始化。
我相信我对二维数组的分配和初始化感到困惑。我认为tmp是const char *数组,我只是想把向量转换成const char **;
那么在代码中,这里使用new和delete是否正确?
我知道,如果2d数组是int*,那么如果我想给它赋值,我需要新的int[num],然后做一个for循环到新的int[];但是我的案子呢?
有人能帮我弄一下这段代码吗?在这种情况下不要使用const,因为您是在初始化后分配数据。
相关文章:
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 为什么通过指针编译时不能分配 const 初始化
- C++ 手动分配和初始化对象
- 静态分配对象的值初始化
- (为什么)我们可以在初始化中将非静态类成员分配给静态变量吗?
- 分配给已删除/未初始化的对象
- C++初始化列表与分配值
- 动态分配的对象未初始化
- 可视化C++将分配移动到未初始化的对象?
- 如何修复初始化后'stack around variable was corrupted.'变量未更改为分配的值
- 在不放置新运算符的情况下,在预分配的内存上使用虚函数初始化对象 - 这可能吗?如果没有,为什么
- 在 C++ 中初始化后的数组分配
- 将已初始化的基类分配给派生类
- 初始化原子指针是原子的吗?如果初始化或内存分配引发,会发生什么情况?
- 当您通过分配初始化C 对象时会发生什么
- 指向未由对象地址初始化的对象的指针如何将值分配给类的数据成员
- 将空初始化器列表分配给现有向量或地图的效果
- 为什么支撑初始化分配分配填充垃圾变量
- 课堂初始化(分配样式)与构造函数性能
- 如何在构造函数中初始化分配的堆可变量