2014-03-05 79 views
-4

我在C++函數應該成員添加到一個鏈表,但是當它執行它拋出一個異常:訪問衝突讀取位置0x0000001

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; 
    } 
} 

這是整個代碼...希望這將有助於。 感謝您的時間:)

+0

怎麼樣給我們提供線路? – Paranaix

+0

你能給我們看看BUNNY嗎? –

+3

我幾乎可以肯定,這個錯誤不在你提供的代碼片段中,但是我們必須看到這個'BUNNY'的聲明。 – pasztorpisti

回答

1

是否有萬阿英,蔣達清或不取決於你如何調用該函數

它應該被稱爲以下方式

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

如果你把它叫做這樣一種方式那麼功能沒有問題。

如果你把它作爲例如

AddBunny(/* some arguments */); 

即不通過返回的指針分配頭,然後頭將總是等於NULL。

編輯:後顯示額外的代碼,那麼我想指出的是,這些語句

BUNNY * head = new BUNNY; 
head = NULL; 

無效。有內存泄漏。你首先分配內存,然後頭部獲取內存地址,然後重新分配頭部。

必須有

BUNNY * head = NULL; 

而且似乎你的代碼有一個錯字。您定義的私有數據成員

bool colour; 
int radioactive_mutant_vampire_bunny; 

但功能AddBunny的相應參數diffirently

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

定義的異常終止是由於不正確格式化的象徵。您將顏色定義爲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 %shead->colour的類型爲bool

由地址00000001消息

Access violation reading location 0x00000001 

中顯示的方式是等於布爾值真的是色值到1。

+0

也許我的視力不好,但我讀:'head = AddBunny(頭部,性別,名字,顏色,放射性);' – Paranaix

+0

你可以刪除你的帖子的第一部分*真的*不相關了嗎? – zmo

+0

@Paranaix原來的帖子被更改了。我知道這個想法不可能出現在你的頭上。 –

-2

你的問題是printf聲明,考慮以下因素:

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

這將產生一個崩潰,因爲printf是假設一個字符串,但您的整數關口。因此,該整數將被解釋爲char*,因此只要printf嘗試打印任何字符(需要解引用指針),就會導致訪問衝突。

但這是,如果你做的到底是什麼情況:

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

由於BUNNY::sex被聲明爲int類型。