Reputation: 141
I have a problem in my C++ code, look at this function:
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;
is performed, Visual Studio says:
Unhandled exception at 0x53eacafa (msvcr100d.dll) in TP6.exe:
Look at my struct titem:
struct titem {
int prioridade;
string nome;
int freq;
};
In Dev-C++ the code works ok! What might case the problem and how do I solve it?
Upvotes: 1
Views: 257
Reputation: 613461
You are using malloc
to allocate an memory for an object. 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
.
Instead of using malloc you need to use new
.
tlista *aux = new tlista;
When you are done with the struct, use delete
to dispose of it.
delete aux;
Since you are using C++ you should forget all about malloc
and free
. Heap allocations are performed with new
and delete
in C++.
Upvotes: 3