简体   繁体   English

Visual Studio 2010上的访问冲突

[英]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 ++,因此您应该忘记所有关于mallocfree Heap allocations are performed with new and delete in C++. 使用C ++中的newdelete执行堆分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cv :: SimpleBlobDetector detect()在Visual Studio 2010中产生访问冲突异常 - cv::SimpleBlobDetector detect() produce access violation exception in Visual Studio 2010 创建连接时,Visual Studio 2010报告Oracle OCCI 11g的“访问冲突” - Visual studio 2010 reporting “Access violation” for Oracle OCCI 11g when creating connection 第一次机会异常……:访问冲突读取位置……使用 Visual Studio 2010 进行调试时 - First-chance exception at …: Access violation reading location … when debugging using Visual Studio 2010 Visual Studio 2017版本内部版本中的Eigen cwisemin / cwisemax访问冲突 - Eigen cwisemin/cwisemax access violation in Visual Studio 2017 release build Visual Studio 2012下奇怪的ifstream访问冲突 - Strange ifstream access violation under Visual Studio 2012 访问冲突读取位置(Visual Studio C ++) - Access violation reading Location (Visual Studio C++) Visual Studio 图形调试器引发读取访问冲突异常 - Visual Studio Graphics Debugger throws read access violation exception C++ 奇怪的访问冲突 Visual Studio 2015 RC - C++ strange access violation Visual studio 2015 RC 字符串长度导致访问冲突值-Visual Studio C ++ - String length causes access violation values -Visual Studio C++ 在控制台应用程序(Visual Studio)中使用VTK-访问冲突 - Using VTK in console application (visual studio) - access violation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM