简体   繁体   English

为其他变量分配值时,C 结构变量被覆盖

[英]C struct variable gets overwritten when assigining other variable a value

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" .我的问题是,当我分配u->id, u->login ... 的值时直到u->created_at ,工作正常,但在我分配u->followers 后u->id将其值更改为“\\ 307UUUU” The same happens after when assigning set_user_public_gists(u, p);分配set_user_public_gists(u, p);也会发生同样的情况 , login changes its value from login = 0x55555555c6c0 "lorraine94588" to login = 0x55555555c6c0 "\\240\\307UUUU" ; , login将其值从login = 0x55555555c6c0 "lorraine94588"更改为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) 

You allocate not enough bytes (I assume that USER is a pointer).您分配的字节不足(我假设 USER 是一个指针)。 Rather than reserving bytes for struct user you allocate only enough bytes to handle a pointer to struct user .您只分配足够的字节来处理指向struct user指针,而不是为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)不要在 typedef 后面隐藏指针(函数指针除外)
  • use the actual object in sizeof rather than type:sizeof使用实际对象而不是类型:
x = malloc(sizeof *x);

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

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