簡體   English   中英

為其他變量分配值時,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 "\\240\\307UUUU"

為什么會發生,我該如何解決?

用戶名

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++;
    }
}

廣交會

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) 

您分配的字節不足(我假設 USER 是一個指針)。 您只分配足夠的字節來處理指向struct user指針,而不是為struct user保留字節。

而不是:

USER u = malloc(sizeof(USER));

它應該是

USER u = malloc(sizeof *u);

對未來的兩個提示:

  • 不要在 typedef 后面隱藏指針(函數指針除外)
  • sizeof使用實際對象而不是類型:
x = malloc(sizeof *x);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM