在过去的 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 文件第二行!
else if (_Traits::eq_int_type(_Traits::eof(),
overflow(_Traits::to_int_type(*_Ptr))))
break; // single character put failed, quit
else
//...
谁能帮帮我,我将不胜感激!!
感谢您的尝试!!
最佳答案
ls_client LS_CLIENT = (ls_client) malloc (sizeof(client_card));
LS_CLIENT = NULL;
您正在创建客户端,然后将其设置为 NULL。您的错误是访问冲突错误。基本上,您正在尝试处理 NULL 指针。交换这些线,应该没问题。
编辑:有人编辑了您的代码,使其不再包含此错误。如果这是你的方式,请忽略这个答案(很抱歉混淆了答案和评论)。
关于c++ - 访问冲突写入位置 0xcccccccc 使用字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15797483/