动态初始化字符的 2D 数组时"Segmentation Fault [Core Dumped]"
"Segmentation Fault [Core Dumped]" while initializing 2d array of char dynamically
我想从user获取一个字符串数组。我得到的例外"分割故障[核心转储]"在运行时。
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std;
int main() {
long testCaseCount = 0;
cin >> testCaseCount;
char **testCases;
*testCases = new char[testCaseCount];
for(int i=0;i<testCaseCount;i++) {
cin >> testCases[i];
}
}
您没有为每个正在读取的字符串分配空间。下面是您正在尝试的两种方法,第一种是您似乎想采用的主要使用c的方法,第二种是充分利用标准库的所有优点。
下面的两个示例应该在给定相同输入内容的情况下产生相同的测试字符串。第一个会随着每个新的额外字符的到来而调整大小。虽然看起来有点矫枉过正,但它实际上比维护一个几何增长算法更简单。
也就是说,下面是代码。我让您来决定哪一个更容易出现错误和bug(我只是在网上写了这两个,所以无论如何肯定会有bug)。
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main()
{
unsigned int testCaseCount = 0;
char **testCases = NULL;
// read and validate we received a count greater than zero
if (cin >> testCaseCount && testCaseCount > 0)
{
// allocate + nullinitialize that many pointers
testCases = new char *[testCaseCount]();
for (unsigned int i = 0; i < testCaseCount && cin; ++i)
{
// skip leading whitespace
char ch;
while (cin.get(ch) && std::isspace(ch));
if (cin)
{
// read chars until whitespace or EOF. vsize
// represents the size of the allocated buffer
char *value = new char[1]();
size_t vsize = 1;
while (cin.get(ch) && !std::isspace(ch))
{
// allocate larger buffer
char *tmp = new char[vsize + 1];
// copy in old content to new buffer
std::copy(value, value + vsize, tmp);
std::swap(value, tmp);
// save new char and terminator
value[vsize - 1] = ch;
value[vsize++] = 0;
// delete old buffer
delete[] tmp;
}
// save accumulated value.
testCases[i] = value;
}
}
}
// show test cases
for (unsigned int i = 0; i < testCaseCount && testCases[i]; ++i)
std::cout << testCases[i] << 'n';
// cleanup
for (unsigned int i = 0; i < testCaseCount && testCases[i]; ++i)
delete[] testCases[i];
delete[] testCases;
return 0;
}
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
int main()
{
unsigned int testCaseCount = 0;
std::vector<std::string> testCases;
if (cin >> testCaseCount)
{
std::string s;
while (testCaseCount-- && cin >> s)
testCases.emplace_back(s);
}
// show test cases
for (auto const& s : testCases)
std::cout << s << 'n';
return 0;
}
你必须至少写为
char **testCases = new char *;
*testCases = new char[testCaseCount];
虽然不清楚为什么你不想简单地写成
char *testCases = new char[testCaseCount];
并且不要忘记删除用new操作符分配的内容。
请注意它不是"字符串数组"。它只是一个字符数组。如果你想得到一个字符串数组,你应该首先决定字符串的最大长度。
首先需要为每个字符串的第一个字符的指针分配空间:
char** testCases = new char*[testCaseCount];
那么你需要为每个字符串分配空间:
testCaseCount[i] = new char[maxStringLength];
cin >> testCaseCount[i];
然而,这是危险的——cin不会做任何边界检查。你真的应该使用std::string.
运行时异常"Segmentation Fault [Core dumps] .
通过解引用:
中的未定义指针来实现未定义行为。*testCases = new char[testCaseCount];
目标:我想从用户
中获得字符串数组
在c++中使用std::string
和std::vector
:
#include <iostream>
#include <string>
#include <vector>
int main() {
long testCaseCount = 0;
std::cin >> testCaseCount;
std::vector<std::string> testCases(testCaseCount);
for (auto& s : testCases)
std::cin >> s;
}
现场演示
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 循环时无法进入:"Segmentation fault"
- OpenGL "Segmentation fault"错误
- 如何修复访问动态数组中结构中的字符串变量时"segmentation fault (core dumped)"错误
- 如何修复 C++ 中的'Segmentation fault'错误
- 创建一个简单的类及其变量和函数不断返回" segmentation fault (core dumped)"、C++
- 在 Mac OS 上遇到的"Segmentation fault: 11" [C++]
- 在 Linux 上实施英特尔 MKL 的 DFT 时"Segmentation fault"
- 尝试读取包含 ints 并存储在 int vector 中的文件,但我不断收到错误"Segmentation fault (core dumped)"
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 我正在尝试通过说话传递一些字符串,它会读取它们,但我得到了"segmentation fault"
- 为什么当我更改此代码时会给我"segmentation fault"错误?
- 尝试将文本文件加载到动态分配的 2D 数组中时出现'Segmentation fault'错误
- 将double打印到std::cout会导致Segmentation fault(C++)
- 将指针分配给链接列表节点会引发"Segmentation Fault"
- "Segmentation fault"使用 Cmake 在C++代码中运行 python 函数时
- 无法读取 Ubuntu "Segmentation fault (core dumped)"中的.dat或.bin文件
- 如何修复C++中的"Segmentation fault(core dump)"?