简体   繁体   English

C ++ Segfault附加,push_back和运算符=或+ =

[英]C++ Segfault appending, push_back and operator = or +=

I have a pretty simple code to read from the file and append chars to the strings and make an array of the struct.I initialize the strings with an init method and use push_back(char) to append to the string but both initialization and push_back gives me segfault. 我有一个非常简单的代码从文件中读取并将chars附加到字符串并构成结构的数组。我使用init方法初始化字符串,并使用push_back(char)附加到字符串,但初始化和push_back都给出了我segfault。 I tought maybe this was a compiler problem so i downloaded a fresh installation of cygwin still didn't work.I tried MinGW and in MinGW i still got an error but i couldn't see it because when debugging nothing worked, in the console printf's didn't show up and cin didn't work so i wasn't able to proceed to the initialization and could only see error when running the code.As a last result i tried Visual Studio and it didn't even compile my code because i didn't had constant array size. 我坚信这可能是编译器问题,所以我下载了新版的cygwin仍然无法正常工作。我尝试了MinGW,但在MinGW中仍然出现错误,但是我看不到它,因为在调试时没有任何效果,在控制台printf的没有出现并且cin无法正常工作,所以我无法进行初始化,只能在运行代码时看到错误。最后,我尝试了Visual Studio,它甚至没有编译我的代码,因为我没有恒定的数组大小。

typedef struct Employee_S {
    string ID, name, surname;
    int age;
    double salary;
} *Employee, Employee_t[1];

Employee employee_init() {
    Employee employee = (Employee) malloc(sizeof (Employee_t));
    if (employee == NULL) {
        printf("Error @ linked_arraylist_init: Cannot allocate memory. (Initialize Error)\n");
    } else {
        (employee->ID) = "";
        (employee->name) = "";
        (employee->surname) = "";
    }
    return employee;
}

I have tried initialization with +=, append, =, push_back all of them gives segfault 我已经尝试使用+ =,append,=,push_back进行初始化,所有这些都为segfault

And this is the stack dump it creates 这是它创建的堆栈转储

Exception: STATUS_ACCESS_VIOLATION at rip=0057B1CAF9F
rax=3FFFFFFFFFFFFFF9 rbx=0000000600062BF0 rcx=0000000000000000
rdx=000000010040407A rsi=0000000000000000 rdi=000000000023A3E0
r8 =0000000000000000 r9 =000000010040407A r10=0000000000240000
r11=00000001004010E2 r12=0000000000000001 r13=0000000000000000
r14=0000000000000001 r15=0000000000000000
rbp=000000000023A340 rsp=000000000023A2C0
program=D:\Programming Stuff\asd\dist\Debug\Cygwin_4.x-Windows\asd.exe, pid 3320, thread main
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame        Function    Args
0000023A340  0057B1CAF9F (00000000000, 0000023AB2F, 10000000023A39E, 0000023AAD0)
0000023A340  0010040110E (0057B1C65FE, 00000000000, 00000000000, 0000023A8E0)
0000023A3D0  00100401783 (0000023A3F0, 0000023A800, 00100000020, 0000023A9FB)
0000023A460  0010040224E (00000000020, FF0700010302FF00, 0018004819A, 00000000000)
0000023AB90  0018004820B (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  0018004611B (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  00180046274 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  001004027A1 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  00100401010 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  7FFECA3416AD (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  7FFECC494629 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace

I'd be glad if i could get some help on this matter. 如果我能在这件事上得到一些帮助,我会很高兴。

C malloc only allocate memory for employee, it does not construct objects, so members like strings are not constructed properly. C malloc只为雇员分配内存,它不构造对象,因此诸如字符串之类的成员构造不正确。

You should use new/delete in this case: 在这种情况下,您应该使用new/delete

Employee employee = new Employee_S();

Also there is no point to use pointer, code is simpler, safer: 同样没有必要使用指针,代码更简单,更安全:

struct Employee_S {
    string ID;
        string name; 
        string surname;
    int age;
    double salary;
};

to default init Employee_S members you simply write 您只需编写默认的初始Employee_S成员

 Employee_S  es = {};

You must to use operator new instead of C function malloc . 您必须使用运算符new而不是C函数malloc When you call malloc objects of type std::string are not created. 调用malloc对象时,不会创建std::string类型的对象。 Function malloc does not call constructors of objects. 函数malloc不调用对象的构造函数。

Function employee_init could look the following way 函数employee_init可能如下所示

Employee employee_init() {
    Employee employee = new Employee_t();
    return employee;
}

Also if you arre going to use an array of type Employee_S then it would be better to use std::vector In this case the function is not required. 同样,如果您打算使用Employee_S类型的数组,那么最好使用std::vector在这种情况下,不需要该函数。 You could simply define 您可以简单地定义

std::vector<Employee_S> employee;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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