C++ - 程序接收信号 SIGSEGV,分段 fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll)
C++ - Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll)
我正在回到 C++ 并且我正在使用 code::blocks。一切都很顺利,但不知从哪里开始出现这个错误
'Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll)
'
代码编译没有错误,但是当我选择选项 1“[1] - ...”时它会抛出该错误。
我用谷歌搜索了一下,但找不到任何相关内容或可以帮助我解决此问题的内容。
我的代码是这样的:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int SeeData();
int ChangeData();
string AddTodo();
class Data {
public:
string name;
string surname;
string age;
string salary;
string todo[];
};
string dataArr[] = {"Bernardo", "Piedade", "19", "540.50"};
string todoArr[] = {"Create new functions"};
int sizeOfTodoArr = sizeof(todoArr) / sizeof(*todoArr);
int main() {
int _menuInput;
while (_menuInput != 0) {
cout << "[1] - See data\n[2] - Change data\n[3] - Add TODO\n[0] - "
"Exit\n\n>>";
cin >> _menuInput;
if (_menuInput == 1) {
SeeData();
} else if (_menuInput == 2) {
ChangeData();
} else if (_menuInput == 3) {
AddTodo();
} else if (_menuInput == 0) {
system("exit");
} else {
cout << "Option not available";
system("cls");
}
}
return 0;
}
int sizeOfData = sizeof(dataArr) / sizeof(*dataArr);
int SeeData() {
Data _myData;
_myData.name = dataArr[0];
_myData.surname = dataArr[1];
_myData.age = dataArr[2];
_myData.salary = dataArr[3];
string person[sizeOfData];
for (int i = 0; i < sizeOfTodoArr; i++) {
_myData.todo[i] = todoArr[i];
}
for (int i = 0; i < sizeOfData; i++) {
person[i] = dataArr[i];
}
for (int i = 0; i < sizeOfData; i++) {
cout << "Name: " << person[0] << "\nSurname: " << person[1]
<< "\nAge: " << person[2] << "\nSalary: " << person[3] << "\n\n";
cout << "Todo List: \n\t";
for (int j = 0; j < sizeOfTodoArr; j++) {
cout << "\n\t" << todoArr[j];
}
}
}
int ChangeData() {}
string AddTodo() {}
编辑:如 u/Uninitialized 所说,错误发生是因为数组 todo[] 没有起始大小。
(0xC0000005) 错误代码表示内存访问冲突。错误消息指出 msvcrt.dll 文件中的 memcpy() 例程出现分段错误,即使您没有明确使用该指令。 class Data 中的 todo[] 成员似乎很可疑,因为它没有分配固定大小(不明白你的编译是如何成功的,但你应该将它初始化为已知大小并检查)
我正在回到 C++ 并且我正在使用 code::blocks。一切都很顺利,但不知从哪里开始出现这个错误
'
Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll)
'
代码编译没有错误,但是当我选择选项 1“[1] - ...”时它会抛出该错误。
我用谷歌搜索了一下,但找不到任何相关内容或可以帮助我解决此问题的内容。
我的代码是这样的:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int SeeData();
int ChangeData();
string AddTodo();
class Data {
public:
string name;
string surname;
string age;
string salary;
string todo[];
};
string dataArr[] = {"Bernardo", "Piedade", "19", "540.50"};
string todoArr[] = {"Create new functions"};
int sizeOfTodoArr = sizeof(todoArr) / sizeof(*todoArr);
int main() {
int _menuInput;
while (_menuInput != 0) {
cout << "[1] - See data\n[2] - Change data\n[3] - Add TODO\n[0] - "
"Exit\n\n>>";
cin >> _menuInput;
if (_menuInput == 1) {
SeeData();
} else if (_menuInput == 2) {
ChangeData();
} else if (_menuInput == 3) {
AddTodo();
} else if (_menuInput == 0) {
system("exit");
} else {
cout << "Option not available";
system("cls");
}
}
return 0;
}
int sizeOfData = sizeof(dataArr) / sizeof(*dataArr);
int SeeData() {
Data _myData;
_myData.name = dataArr[0];
_myData.surname = dataArr[1];
_myData.age = dataArr[2];
_myData.salary = dataArr[3];
string person[sizeOfData];
for (int i = 0; i < sizeOfTodoArr; i++) {
_myData.todo[i] = todoArr[i];
}
for (int i = 0; i < sizeOfData; i++) {
person[i] = dataArr[i];
}
for (int i = 0; i < sizeOfData; i++) {
cout << "Name: " << person[0] << "\nSurname: " << person[1]
<< "\nAge: " << person[2] << "\nSalary: " << person[3] << "\n\n";
cout << "Todo List: \n\t";
for (int j = 0; j < sizeOfTodoArr; j++) {
cout << "\n\t" << todoArr[j];
}
}
}
int ChangeData() {}
string AddTodo() {}
编辑:如 u/Uninitialized 所说,错误发生是因为数组 todo[] 没有起始大小。
(0xC0000005) 错误代码表示内存访问冲突。错误消息指出 msvcrt.dll 文件中的 memcpy() 例程出现分段错误,即使您没有明确使用该指令。 class Data 中的 todo[] 成员似乎很可疑,因为它没有分配固定大小(不明白你的编译是如何成功的,但你应该将它初始化为已知大小并检查)