0

過去4日間、私はこのエラーに悩まされていました:

Unhandled exception at 0x77c415de in ls_client_app.exe: 
0xC0000005: Access violation reading location 0xcccccccc.

コードは次のとおりです。

typedef struct client {
    string  ID;
    // some other strings
} client;

typedef struct client_card {
    client CLIENT;
    client_card * next;
} client_card;

typedef client_card * ls_client;

これは add_client 関数です:

int add_client(ls_client &LS_CLIENT) {

    client NEW_CLIENT;
    ls_client NEW_CLIENT_CARD = (ls_client) malloc (sizeof(client_card));

    cout << "CLIENT ID: " <<endl;
    cin  >>  NEW_CLIENT.ID;

    NEW_CLIENT_CARD->next    =  NULL;
    NEW_CLIENT_CARD->CLIENT  =  &NEW_CLIENT;

    while (LS_CLIENT != NULL) {
        LS_CLIENT = LS_CLIENT->next;
    }
    LS_CLIENT = NEW_CLIENT_CARD;

    return 0;
}

これがメインです:

int main() {
    ls_client LS_CLIENT = (ls_client) malloc (sizeof(client_card));
    LS_CLIENT = NULL;
    add_client(LS_CLIENT);

    // Error in this line !
    cout << LS_CLIENT->CLIENT->ID <<endl;

    return 0;
}

streambuf ファイルの 2 行目に移動します。

else if (_Traits::eq_int_type(_Traits::eof(),
    overflow(_Traits::to_int_type(*_Ptr))))
    break;  // single character put failed, quit
else
//...

誰でも私を助けてくれませんか。感謝します!!
試してくれてありがとう!!

4

1 に答える 1

7

ls_client LS_CLIENT = (ls_client) malloc (sizeof(client_card)); LS_CLIENT = NULL;

クライアントを作成し、それを NULL に設定しています。あなたのエラーはアクセス違反エラーです。基本的に、NULL ポインターで作業しようとしています。それらの行を交換すると、問題ないはずです。

編集: 誰かがあなたのコードを編集して、このエラーが含まれないようにしました。これがあなたのやり方である場合は、この回答を無視してください(回答とコメントを混同して申し訳ありません)。

于 2013-04-03T20:32:45.193 に答える