简体   繁体   中英

C++ - Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\Windows\System32\msvcrt.dll)

I'm getting back to c++ and I'm using code::blocks. Everything was going fine but out of nowhere I started to get this error

' Program received signal SIGSEGV, Segmentation fault.In msvcrt!memcpy () (C:\\Windows\\System32\\msvcrt.dll) '

The code compiles with no errors, but when I choose the option 1 "[1] - ... " it throws that error.

I googled a bit but I couldn't find anything related or that could help me fix this problem.

My code it's this:

    #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() {}

Edit: As u/Uninitialized said, the error was happening because the array todo[] didn't had a starting size.

(0xC0000005) error code denotes a memory access violation. The error message says segmentation fault that arises from the memcpy() routine in the msvcrt.dll file, even though you haven't used the instruction explicitly. The todo[] member in class Data seems suspicious, since it has no fixed size allocated (don't understand how your compilation was successful, but you should probably initialize it to a known size and check)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM