سؤال

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. 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.

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

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.

You should use new/delete in this case:

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  es = {};

نصائح أخرى

You must to use operator new instead of C function malloc. When you call malloc objects of type std::string are not created. Function malloc does not call constructors of objects.

Function employee_init could look the following way

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. You could simply define

std::vector<Employee_S> employee;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top