分段错误(核心转储)将窗口发布到 linux
Segmentation Fault (Core Dumped) issue windows to linux
我一直在Windows上使用Visual Studio进行编程,我的代码工作正常,但是在Ubuntu上运行它时,我得到了一个
分段故障(核心转储)
问题,我不知道是什么原因造成的。我不知道问题在代码中的哪个位置,所以我粘贴了所有问题。
#include "Person.h"
#include "Grade.h"
using std::ifstream;
using std::ofstream;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::stringstream;
using std::fixed;
using std::setprecision;
using std::setfill;
unsigned z = 0;
unsigned y = 0;
void firstInput(vector<Person>& peoples, char *argv[])
{
int x = 0;
string idy, namey, addressy, phoney, s;
ifstream one;
one.open(argv[1]);
while (getline(one, s))
{
if (x == 0)
{
idy = s;
}
if (x == 1)
{
namey = s;
}
if (x == 2)
{
addressy = s;
}
if (x == 3)
{
phoney = s;
Person *f1 = new Person(idy, namey, addressy, phoney);
peoples.push_back(*f1);
x = -1;
}
x++;
}
one.close();
}
void secondInput(vector<Grade>& gradies, char *argv[])
{
int b;
string z, idy2, gradey, classy;
ifstream two;
two.open(argv[2]);
b = 0;
while (getline(two, z))
{
if (b == 0)
{
classy = z;
}
if (b == 1)
{
idy2 = z;
}
if (b == 2)
{
gradey = z;
Grade *g1 = new Grade(classy, idy2, gradey);
gradies.push_back(*g1);
b = -1;
}
b++;
}
two.close();
}
double scoreConverter(string lettergrade)
{
double converted = 0.0;
if (lettergrade.substr(0,1) == "A")
{
converted += 4;
}
if (lettergrade.substr(0,1) == "B")
{
converted += 3;
}
if (lettergrade.substr(0,1) == "C")
{
converted += 2;
}
if (lettergrade.substr(0,1) == "D")
{
converted += 1;;
}
if (lettergrade.size() > 1)
{
if (lettergrade.substr(1,2) == "-")
{
converted -= 0.3;
}
if (lettergrade.substr(1,2) == "+")
{
converted += 0.4;
}
}
return converted;
}
void computeGPA(vector<Grade>& s, vector<Person>& p, string IDnum)
{
int count = 0;
y = 0;
double gpa = 0;
for (string x = IDnum; y < s.size(); y++)
{
if (x == s.at(y).getIDs())
{
gpa += scoreConverter(s.at(y).getScore());
count++;
}
}
if (gpa > 0)
{
gpa = gpa / count;
}
cout << IDnum << " ";
cout << fixed << setprecision(2) << setfill('0') << gpa << " ";
for (unsigned x = 0; x < p.size(); x++)
{
if (IDnum == p.at(x).getID())
{
cout << p.at(x).getName() << endl;
x = 1000;
}
}
z = y;
}
void thirdInput(vector<Grade>& gradies, vector<Person>& persons, char *argv[])
{
string querying;
ifstream three;
three.open(argv[3]);
while(getline(three, querying))
{
for (unsigned x = 0; x < persons.size(); x++)
{
if (querying == persons.at(x).getID())
{
computeGPA(gradies, persons, persons.at(x).getID());
x = 1000;
}
}
}
}
int main(int argc, char *argv[]) {
ofstream outputit;
outputit.open(argv[4]);
std::ofstream out(argv[4]);
std::cout.rdbuf(out.rdbuf());
vector<Person> people;
vector<Grade> grades;
firstInput(people, argv);
std::sort(people.begin(), people.end());
for (unsigned x = 0; x < people.size(); x++)
{
cout << people.at(x).getName() << endl;
cout << people.at(x).getID() << endl;
cout << people.at(x).getPhone() << endl;
cout << people.at(x).getAddress() << endl;
}
cout << endl;
secondInput(grades, argv);
std::sort(grades.begin(), grades.end());
for (unsigned x = 0; x < grades.size(); x++)
{
cout << grades.at(x).getIDs() << " ";
cout << grades.at(x).getScore() << " ";
cout << grades.at(x).getClass() << endl;
}
cout << endl;
thirdInput(grades, people, argv);
outputit.close();
}
为了找出分段错误发生的位置,您可以使用gdb
.
使用 -ggdb
编译程序,假设您使用的是 GCC。
使用以下命令运行程序:
gdb myprog myinputs.txt
进入提示gdb
后,键入 run
。一旦流程分段出错,您应该返回到gdb
提示符。从那里,您可以键入bt
以获取回溯。
这应该为您提供足够的信息来调查您的错误。
发生这种情况是因为默认情况下提供给 g++ 编译器的内存是堆栈的 8192 KB,而 Windows 计算机并非如此。您可以通过在终端中键入以下命令来检查发行版可用的内存:
ulimit -s
ulimit 是每个进程打开的文件描述符的数量。因此,初始化变量的方式使程序不会使用超过 8192 KB 的内存,例如使用 short int
代替int
(请记住,溢出不会发生)。或者,您可以将堆栈的 ulimit 设置为无限制,并在终端中输入以下内容后编译相同的代码:
ulimit -s unlimited
代替unlimited
,您也可以分配自定义内存,例如,如果要分配 10000 KB,则应写入ulimit -s 10000
。
我希望这能解决问题。
相关文章:
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- QtCUrl post不再工作(Linux nok...窗口确定)
- Qt - 有没有办法检查窗口是否标记为始终在顶部(linux)?
- 如何确保在窗口和 Linux 上具有相同的浮点行为
- 当模式窗口最小化时,最小化所有应用程序窗口(在 Linux 上)
- Linux中的SDL2空透明窗口
- 在没有任何外部库的情况下,是否可以在c++/linux的窗口中显示bmp图像
- 在窗口中执行 Linux c++ 程序
- 捕获Linux/Ubuntu(屏幕捕获 鼠标/键盘事件)中的窗口事件
- 从基于窗口的应用程序远程运行Linux命令
- 分段错误(核心转储)将窗口发布到 linux
- C++(LINUX)使用DevIL设置X11窗口背景
- 在linux中动态地将应用程序窗口排列在一个又一个之上
- Linux通过pid获取窗口图标
- 如何使用 C++ 在 Linux 中创建窗口
- 如何使用FLTK在Windows、Mac OS X和Linux中使窗口透明
- 使用C++窗口应用程序加载Linux格式的文本文件
- 保持一个应用程序窗口始终在当前桌面在linux和mac
- xterm窗口不能在Linux上保持,它出现然后消失得非常快
- c++ /Linux -绘制到窗口