C++ 创建动态字符串数组时出现Seg错误
我的朋友正在写一个基于文本的游戏,让我看一下这个崩溃的代码。我调试了它,它在创建动态阵列时出现seg故障。我不确定到底是什么原因,我建议他完全避免使用指针,使用向量,希望这能解决他的问题,但我很好奇这里到底出了什么问题。这是他的密码:C++ 创建动态字符串数组时出现Seg错误,c++,segmentation-fault,C++,Segmentation Fault,我的朋友正在写一个基于文本的游戏,让我看一下这个崩溃的代码。我调试了它,它在创建动态阵列时出现seg故障。我不确定到底是什么原因,我建议他完全避免使用指针,使用向量,希望这能解决他的问题,但我很好奇这里到底出了什么问题。这是他的密码: #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <ctime> using na
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class nation
{
public:
void init();
string genName();
string getName();
private:
string myName;
int* myBorderPoints;
};
string nation::getName()
{
return myName;
}
string nation::genName()
{
int listLength = 0, listPos = 0, listRand = 0;
string nameToGen = "";
string* namePartList;
ifstream fileName;
fileName.open("NamePart1.txt");
listLength = fileName.tellg();
namePartList = new string[listLength]; // Seg fault here
while (fileName.good())
{
while (!fileName.eof())
{
getline(fileName,namePartList[listPos]);
listPos += 1;
}
}
listRand = rand() % listLength;
nameToGen += namePartList[listRand];
fileName.close();
listLength = 0;
listPos = 0;
listRand = 0;
nameToGen = "";
fileName.open("NamePart2.txt");
listLength = fileName.tellg();
namePartList = new string[listLength];
while (fileName.good())
{
while (!fileName.eof())
{
getline(fileName,namePartList[listPos]);
listPos += 1;
}
}
listRand = rand() % listLength;
nameToGen += namePartList[listRand];
fileName.close();
return nameToGen;
}
void nation::init()
{
srand(time(NULL));
myName = genName();
}
int main()
{
nation testNation;
testNation.init();
cout << testNation.getName();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
阶级国家
{
公众:
void init();
字符串genName();
字符串getName();
私人:
字符串myName;
int*myBorderPoints;
};
字符串国家::getName()
{
返回我的名字;
}
字符串国家::genName()
{
int listLength=0,listPos=0,listRand=0;
字符串nameToGen=“”;
字符串*namePartList;
ifstream文件名;
打开(“NamePart1.txt”);
listLength=fileName.tellg();
namePartList=新字符串[listLength];//此处存在Seg错误
while(fileName.good())
{
而(!fileName.eof())
{
getline(文件名、名称部件列表[listPos]);
listPos+=1;
}
}
listRand=rand()%listLength;
nameToGen+=namePartList[listRand];
fileName.close();
listLength=0;
listPos=0;
listRand=0;
nameToGen=“”;
打开(“NamePart2.txt”);
listLength=fileName.tellg();
namePartList=新字符串[listLength];
while(fileName.good())
{
而(!fileName.eof())
{
getline(文件名、名称部件列表[listPos]);
listPos+=1;
}
}
listRand=rand()%listLength;
nameToGen+=namePartList[listRand];
fileName.close();
返回nameToGen;
}
void nation::init()
{
srand(时间(空));
myName=genName();
}
int main()
{
国家测试国家;
testNation.init();
你能告诉我你在叫什么吗
listLength = fileName.tellg();
在没有读取任何内容的情况下,根据文件是否成功打开,将返回0
或-1
,因此您将调用:
namePartList = new string[listLength]
可能是一个不需要的值。我很确定它返回了-1
,因为它应该是正常的
这也适用于以后的代码,使用std::vector可能更有意义。您是否尝试将其归结为一个完整的最小示例?您看到了listLength
的值是什么?我猜listLength=fileName.tellg();
正在返回-1
,因为=0
不会seg fault.@ShafikYaghmour Close--他在打开流之后调用它,没有读任何东西。很可能==0。OP可能认为tellg
给了他文件长度。我会说写出来作为答案。fileName.open()可能会失败,因为该文件可能不存在,或者您可能没有正确的权限,或者该文件已被另一个进程锁定。如果打开失败,我将使用tellg()返回-1,这将导致字符串分配崩溃。创建一个包含零个元素的数组是有效的。当然,创建一个包含负数的数组是无效的。如果要解决此类问题,NamePart2.txt也会出现同样的情况。您可能需要引入If-else。例如If(文件名)然后继续…或者只是抛出一个异常