[英]Access Violation on Visual Studio 2010
I have a problem in my C++ code, look at this function: 我的C ++代码有问题,看看这个函数:
void insere(titem x){
tlista *aux;
aux = (tlista*)malloc(sizeof(tlista));
aux->item = x;
ultimo->prox = aux;
ultimo = ultimo->prox;
aux->prox = NULL;
}
When the line: aux->item = x;
当行: aux->item = x;
is performed, Visual Studio says: 是的,Visual Studio说:
Unhandled exception at 0x53eacafa (msvcr100d.dll) in TP6.exe: TP6.exe中0x53eacafa(msvcr100d.dll)的未处理异常:
Look at my struct titem: 看看我的struct titem:
struct titem {
int prioridade;
string nome;
int freq;
};
In Dev-C++ the code works ok! 在Dev-C ++中,代码工作正常! What might case the problem and how do I solve it? 可能出现什么问题,如何解决?
You are using malloc
to allocate an memory for an object. 您正在使用malloc
为对象分配内存。 That will allocate the memory, but it will not initialize the object. 这将分配内存,但不会初始化对象。 That's a problem for the non-POD members, for example aux->item.nome
. 对于非POD成员来说这是一个问题,例如aux->item.nome
。
Instead of using malloc you need to use new
. 而不是使用malloc,你需要使用new
。
tlista *aux = new tlista;
When you are done with the struct, use delete
to dispose of it. 完成结构后,使用delete
来处理它。
delete aux;
Since you are using C++ you should forget all about malloc
and free
. 由于您使用的是C ++,因此您应该忘记所有关于malloc
和free
。 Heap allocations are performed with new
and delete
in C++. 使用C ++中的new
和delete
执行堆分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.