C结构变量在为其他变量赋值时被覆盖

C struct variable gets overwritten when assigining other variable a value

我正在编写一个程序来解析字符串并将其值存储在结构中。

我的问题是,当我分配 u->id 的值时,u->login...直到 u->created_at,工作正常,但在我分配 u->followers 之后,u->id 将其值更改为 " \307UUUU"。 在分配 set_user_public_gists(u, p);login 后,也会发生同样的情况,将其值从 login = 0x55555555c6c0 "lorraine94588"login = 0x55555555c6c0 "07UUUU";

为什么会这样,我该如何解决?

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]
 = {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]
 = {id = 0x55555555c6a0 " 7UUUU", 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) 

您分配的字节数不足(我假设 USER 是一个指针)。 不是为 struct user 保留字节,而是只分配足够的字节来处理 指针 struct user.

而不是:

USER u = malloc(sizeof(USER));

应该是

USER u = malloc(sizeof *u);

未来的两个提示:

  • 不要在 typedef 后面隐藏指针(函数指针除外)
  • 使用 sizeof 中的实际对象而不是键入:
x = malloc(sizeof *x);