Home > Software design >  C struct variable gets overwritten when assigining other variable a value
C struct variable gets overwritten when assigining other variable a value

Time:11-17

I'm writing a program that parses a string and stores its values in a struct.

My problem is, when I assign the value of u->id, u->login... till u->created_at, works fine, but after I assign u->followers, u->id changes its value to " \307UUUU". The same happens after when assigning set_user_public_gists(u, p);, login changes its value from login = 0x55555555c6c0 "lorraine94588" to login = 0x55555555c6c0 "\240\307UUUU";

Why is it happening and how can i solve it?

user.c

struct user {
    char *id;
    char *login;
    char *type;
    char *created_at;
    char *followers;
    char *follower_list;
    char *following;
    char *following_list;
    char *public_gists;
    char *public_repos;
};

USER create_user() {
    USER u = malloc(sizeof(USER));
    return u;
}

void delete_user(USER u) {
    free(u);
}

void set_user_id(USER u, char *s) {
    u->id = strdup(s);
}
void set_user_login(USER u, char *s) {
    u->login = strdup(s);
}
void set_user_type(USER u, char *s) {
    u->type = strdup(s);
}
void set_user_created_at(USER u, char *s) {
    u->created_at = strdup(s);
}
void set_user_followers(USER u, char *s) {
    u->followers = strdup(s);

}void set_user_follower_list(USER u, char *s) {
    u->follower_list = strdup(s);
}
void set_user_following(USER u, char *s) {
    u->following = strdup(s);
}
void set_user_following_list(USER u, char *s) {
    u->following_list = strdup(s);
}
void set_user_public_gists(USER u, char *s) {
    u->public_gists = strdup(s);
}
void set_user_public_repos(USER u,char *s) {
    u->public_repos = strdup(s);
}

void set_user(USER u, char *line) {
    char *p = NULL, *temp_line = line;
    int i = 0;
    while ((p = strsep(&temp_line, ";")) != NULL) {
        switch (i) {
            case 0:
                set_user_id(u, p);
                break;
            case 1:
                set_user_login(u, p);
                break;
            case 2:
                set_user_type(u, p);
                break;
            case 3:
                set_user_created_at(u, p);
                break;
            case 4:
                set_user_followers(u, p);
                break;
            case 5:
                set_user_follower_list(u, p);
                break;
            case 6:
                set_user_following(u, p);
                break;
            case 7:
                set_user_following_list(u, p);
                break;
            case 8:
                set_user_public_gists(u, p);
                break;
            case 9:
                set_user_public_repos(u, p);
                break;
            }
        i  ;
    }
}

GDB

79                  set_user_followers(u, p);
(gdb) print u[0]
$14 = {id = 0x55555555c6a0 "6611157", login = 0x55555555c6c0 "lorraine94588", type = 0x55555555c6e0 "User", 
  created_at = 0x55555555c700 "2014-02-07 01:01:35", 
  followers = 0x37353131313636 <error: Cannot access memory at address 0x37353131313636>, follower_list = 0x0, following = 0x0, 
  following_list = 0x21 <error: Cannot access memory at address 0x21>, 
  public_gists = 0x656e696172726f6c <error: Cannot access memory at address 0x656e696172726f6c>, 
  public_repos = 0x3838353439 <error: Cannot access memory at address 0x3838353439>}
(gdb) n
80                  break;
(gdb) print u[0]
$15 = {id = 0x55555555c6a0 " \307UUUU", login = 0x55555555c6c0 "lorraine94588", type = 0x55555555c6e0 "User", 
  created_at = 0x55555555c700 "2014-02-07 01:01:35", followers = 0x55555555c720 "0", follower_list = 0x0, following = 0x0, 
  following_list = 0x21 <error: Cannot access memory at address 0x21>, 
  public_gists = 0x656e696172726f6c <error: Cannot access memory at address 0x656e696172726f6c>, 
  public_repos = 0x3838353439 <error: Cannot access memory at address 0x3838353439>}
(gdb) 

CodePudding user response:

You allocate not enough bytes (I assume that USER is a pointer). Rather than reserving bytes for struct user you allocate only enough bytes to handle a pointer to struct user.

Rather than:

USER u = malloc(sizeof(USER));

it should be

USER u = malloc(sizeof *u);

Two hints for future:

  • do not hide pointers behind the typedef (except function pointers)
  • use the actual object in sizeof rather than type:
x = malloc(sizeof *x);
  • Related