gpt4 book ai didi

c++ - Visual Studio 2010 上的访问冲突

转载 作者:行者123 更新时间:2023-11-30 02:56:54 28 4
gpt4 key购买 nike

我的 C++ 代码有问题,看看这个函数:

void insere(titem x){
tlista *aux;
aux = (tlista*)malloc(sizeof(tlista));
aux->item = x;
ultimo->prox = aux;
ultimo = ultimo->prox;
aux->prox = NULL;
}

当执行行:aux->item = x; 时,Visual Studio 说:

Unhandled exception at 0x53eacafa (msvcr100d.dll) in TP6.exe:

看看我的结构体:

 struct titem {
int prioridade;
string nome;
int freq;
};

在 Dev-C++ 中,代码工作正常!什么情况下会出现问题?我该如何解决?

最佳答案

您正在使用 malloc 为对象分配内存。这将分配内存,但不会初始化对象。这对非 POD 成员来说是个问题,例如 aux->item.nome

您需要使用 new 而不是 malloc。

tlista *aux = new tlista;

完成结构后,使用delete 来处理它。

delete aux;

由于您使用的是 C++,因此您应该完全忘记 mallocfree。在 C++ 中使用 newdelete 执行堆分配。

关于c++ - Visual Studio 2010 上的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com