【发布时间】:2014-11-09 02:54:27
【问题描述】:
我正在使用 Visual Studio 2013。我收到 Access violation reading location 0xCDCDCDD1 错误。来自Wikipedia: Magic Numbers、here 和this question 的引用告诉我我正在使用未初始化的指针。我认为问题出在我正在使用的 malloc 函数上,尽管在使用它之前我确实参考了 CPlusPlus 文档。
我的代码:
#include "SingleNode.h"
#include "SingleList.h"
#include <iostream>
SingleList::SingleList() {}
SingleList::~SingleList() {}
...
void SingleList::addHead(int value){
ISingleNode *temp1 = (ISingleNode *)malloc(sizeof(SingleList));
head = NULL;
temp1->setValue(value);
temp1->setNext(head->getNext());
if (!head->getNext())
{
head = temp1;
}
else
{
temp1->setNext(head->getNext());
head = temp1;
}
}
...
错误在temp1->setValue(value);这一行
setValue(int value) 函数包含在此处:
#include "SingleNode.h"
SingleNode::SingleNode() {}
SingleNode::~SingleNode() {}
void SingleNode::setValue(int value){
this->value = value;
}
int SingleNode::getValue(){
return value;
}
ISingleNode * SingleNode::getNext(){
return next;
}
void SingleNode::setNext(ISingleNode * next){
this->next = next;
}
单节点.h:
#pragma once
#include "Interfaces01.h"
class SingleNode :public ISingleNode{
private:
int value;
ISingleNode * next = NULL;
public:
SingleNode();
~SingleNode();
void setValue(int value);
int getValue();
ISingleNode * getNext();
void setNext(ISingleNode * next);
};
SingleList.h:
#pragma once
#include "Interfaces01.h"
class SingleList :public ISingleList{
private:
int value;
ISingleNode * head=NULL;
ISingleNode * temp=NULL;
ISingleNode * next=NULL;
ISingleNode * temp1;
public:
SingleList();
~SingleList();
ISingleNode * getHead();
void setHead(ISingleNode * head);
void addHead(int value);
void orderSort2();
void orderSort3();
void sequenceOrderSort();
void reverse();
ISingleNode * swap(ISingleNode * head, int k);
ISingleNode * get_node(ISingleNode * head, int k);
};
在换行时,调试器向我显示以下值:
【问题讨论】:
-
为什么在 C++ 中使用
malloc之类的 C 库调用?
标签: c++ exception visual-studio-2013 heap-memory