jeudi 7 juin 2018

C++ : Bus Error: 10 when assign string passed in method

I am trying to assign a string whose value is passed into the method when I got this error :

Bus error: 10

My code:

struct user {
   string username;
   string password;
};

The method:

user *init_user(const string & username, const string & password){ 
    user *u = (user *)malloc(sizeof(user));
    if (u == NULL){
        return NULL;
    }
    u->username = username;
    u->password = password;
    return u;
 }

Calling:

user *root = init_user("root", "root");

I think the error is raised by

u->username = username;
u->password = password;

The compiler I'm using is c++11

OS: MacOS

Aucun commentaire:

Enregistrer un commentaire