【问题标题】:C struct variable gets overwritten when assigining other variable a value为其他变量分配值时,C struct 变量被覆盖
【发布时间】:2021-12-27 16:15:00
【问题描述】:

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

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

为什么会发生,我该如何解决?

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) 

【问题讨论】:

标签: c struct gdb variable-assignment


【解决方案1】:

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

而不是:

USER u = malloc(sizeof(USER));

应该是

USER u = malloc(sizeof *u);

未来的两个提示:

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

【讨论】:

  • 哦,我忘了指出 USER 是一个指针,但问题是,谢谢!!
猜你喜欢
  • 2021-03-22
  • 1970-01-01
  • 2019-07-23
  • 2010-11-17
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多