读取位置0x0000001时发生访问冲突

Access violation reading location 0x0000001

本文关键字:访问冲突 位置 0x0000001 读取      更新时间:2023-10-16

我在c++中有一个函数,它应该向链表添加一个成员,但当它被执行时,它会抛出一个异常:

Unhandled exception at 0x5b9414cf (msvcr100d.dll) in Sample.exe: 0xC0000005: Access violation reading location 0x00000001

当某些变量不确定时,就会发生这种情况。。。但我不知道这个变量在哪里。。。也带有断点。这里的整个代码(除了.h文件…但它只包含声明)

#include "stdafx.h"
#include "winsock.h"
#include "windows.h"
#include <string.h>
#include <iostream>
#include "Sample.h"
using namespace std;
struct BUNNY 
{
public:
int sex;
int name;
int age;
bool colour;
int radioactive_mutant_vampire_bunny;
BUNNY *nextBunny;
};

int returnSex();
int returnName (int sex); 
int returnColour ();
bool radioactiveBunny ();
string translateName(int name);
string translateSex(int sex);
string translateColour(int colour);
BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny);
BUNNY * travereseBunny(BUNNY * head);
BUNNY * displayBunny();
int _tmain(int argc, _TCHAR* argv[])
{
bool flag = true;
int turn = 0;
BUNNY * head = new BUNNY;
head = NULL;
while (flag)
{
++turn;
if (turn == 1)
{
for (int i = 1; i <= 5; i++)
{
int sex = returnSex();
int name = returnName(sex);
int colour = returnColour();
bool radioactive = radioactiveBunny();
head = AddBunny(head,sex,name,colour,radioactive);
printf("A new bunny is born: sex %s , name %s , colour %s , radioactive %d",head->sex,head->name,head->colour,head->radioactive_mutant_vampire_bunny);
system("pause");
}

}
}
return 0;
}
int returnSex()
{
int random = rand() % 100 + 1;
if(random > 50)
return MALE;
else
return FEMALE;
}
int returnName(int sex)
{
if (sex == MALE)
{
int random = (rand() % 100 + 1) / 5;
if(random < 20)
return BU;
else if (random > 20 && random <40)
return CRYSTAL;
else if(random > 40 && random < 60)
return JASON;
else if(random > 60 && random < 80)
return ERO;
else if(random > 80)
return METH;
}
else 
{
int random1 = rand() % 100 + 1;
if (random1 < 50 )
{
return MARIA;
}
else if (random1 > 50)
return JAMIE;
}
}
int returnColour ()
{
int random = rand() % 4 + 1;
if(random == 1)
return WHITE;
else if(random > 1 && random <= 2)
return BROWN;
else if(random > 2 && random <= 3)
return BLACK;
else if(random > 3 && random <= 4)
return SPOTTED;
}
bool radioactiveBunny()
{
int random = rand() % 100 + 1;
if(random > 0 && random <= 2)
return true;
else 
return false;
}
BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny)
{
BUNNY * newBunny = new BUNNY;
newBunny->age = 0;
newBunny->colour = colour;
newBunny->sex = sex;
newBunny->name = name;
newBunny->radioactive_mutant_vampire_bunny = radioactive_mutant_vampire_bunny;
newBunny->nextBunny = head;
return newBunny;
}
string translateName (int name)
{
switch (name)
{
case CRYSTAL: return "CRYSTAL";
break;
case BU: return "BU";
break;
case JASON: return "JASON";
break;
case ERO: return "ERO";
break;
case METH: return "METH";
break;
case MARIA: return "MARIA";
break;
case JAMIE: return "JAMIE";
break;
}
}
string translateColour (int colour)
{
switch (colour)
{
case WHITE: return "WHITE";
break;
case BLACK: return "BLACK";
break;
case BROWN: return "BROWN";
break;
case SPOTTED: return "SPOTTED";
break;
}
}
string translateSex (int sex)
{
switch (sex)
{
case MALE: return "MALE";
break;
case FEMALE: return "FEMALE";
break;
}
}

这是整个代码。。。希望它能有所帮助。感谢您抽出时间:)

是否存在问题取决于如何调用函数

它应该被称为以下方式

head = AddBunny( /* some arguments */ );

如果你这样调用它,那么函数就没有问题。

如果你把它称为

AddBunny( /* some arguments */ );

也就是说,如果不通过返回指针指定head,那么head将始终等于NULL。

编辑:在您展示了额外的代码之后,我想指出这些语句

BUNNY * head = new BUNNY;
head = NULL;

无效。内存泄漏。首先分配内存,head获取内存的地址,然后重新分配head。

必须有

BUNNY * head = NULL;

此外,您的代码似乎有拼写错误。您将私人数据成员定义为

bool colour;
int radioactive_mutant_vampire_bunny;

但是函数AddBunny的相应参数难以定义

BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny);

异常终止是由于符号格式不正确。您将color定义为bool,但试图将其输出为字符串文字。

printf("A new bunny is born: sex %s , name %s , colour %s , radioactive %d",head->sex,head->name,head->colour,head->radioactive_mutant_vampire_bunny);

您编写了colour %s,而head->colour的类型为bool

顺便说一下,消息中显示的地址0x00000001

Access violation reading location 0x00000001 

是等于布尔值true的color值,该值等于1。

您的问题是printf语句,请考虑以下内容:

printf("Test: %s", 2);

这将产生崩溃,因为printf假定为字符串,但您传递了一个整数。因此,该整数将被解释为char*,因此一旦printf尝试打印任何字符(这需要取消引用指针),就会导致访问违规。

但如果你这样做,就会发生这种情况:

printf("A new bunny is born: sex %s", head->sex);

由于BUNNY::sex被声明为内部