2011-05-03 77 views
0

我在一個相對基本的編程類,我得到一個我從來沒有遇到過的錯誤。它是這樣的:調試斷言在刪除分配的內存失敗

Debug Assertion Failed! 

Program:...sual Studio 2010\Projects\Comp Project\Debug\Comp Project.exe 
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp 
Line: 52 

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) 

這裏是我的代碼和分配的內存被刪除,它看起來錯誤彈出像

//Shortest Path Version 1.01 IN PROGRESS 
/* 
    This program reads in node and arc data and interpretes it into a list which it can then use 
    to calculate the shortest time or distance, depending on user prefrence, between two chosen 
    nodes and send pack the path to the user 
*/ 
#include <conio.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
void main() 
{ 
    //opening the files where the data is contained. 
    ifstream arcfile; 
    ifstream nodefile; 
    arcfile.open("G:\\Programming\\Descrete Structures\\arcs2011.txt"); 
    nodefile.open("G:\\Programming\\Descrete Structures\\nodes2011.txt"); 

    //creating a class that will store the data relevent to locating the shortest path 
    class Arc 
    { 
    public: 
     unsigned short int FROM;        //from node 
     unsigned short int TO;         //to node 
     double DISTANCE;          //distance in miles 
     double TIME;           //travel time 
    }; 

    //creating a class that will store the data relavent to the node path 
    class Node 
    { 
    public: 
     unsigned long int WIEGHT;        //either time or distance 
     unsigned short int FROM;        //alternativly "via" 
     bool* shortestKnown; 

    }; 

    string s;             //placeholder for irrelavent string data 
    unsigned short int StartingNode;       //user selected starting node 
    unsigned short int EndingNode;        //user selected ending node 
    unsigned short int ARCSIZE = 0;        //number of Arcs in arc file 
    unsigned short int NODESIZE = 0;       //number of Nodes in node file 

    //////////////////////Begin reading in of Arc data and Node data///////////////////// 

    //count number of registered arcs 
    while(getline(arcfile,s)) 
    { 
     ARCSIZE++; 
    } 
    cout<<ARCSIZE<<" line size of arcfile"<<endl; 

    //count number of registered nodes 
    while(getline(nodefile,s)) 
    { 
     NODESIZE++; 
    } 
    NODESIZE++;             //final incrementation for +1 format 
    cout<<NODESIZE<<" line size of nodefile"<<endl; 

    Arc* Arclist;            //array that will store all the arcs 
    Arclist = new Arc[ARCSIZE];         //based on the size of the file 
    string* Nodelist;           //array that will store the node names 
    Nodelist = new string[NODESIZE];       //based on the size of the file 

    //reset the streams 
    arcfile.close(); 
    nodefile.close(); 
    arcfile.open("G:\\Programming\\Descrete Structures\\arcs2011.txt"); 
    nodefile.open("G:\\Programming\\Descrete Structures\\nodes2011.txt"); 


    //loop through and save the arc data to an array 
    for(int i=1;i<ARCSIZE;i++) 
    { 
     arcfile.ignore(1000,'\n'); 
     arcfile>>Arclist[i].FROM; 
     arcfile>>Arclist[i].TO; 
     arcfile>>Arclist[i].TIME; 
     arcfile>>Arclist[i].DISTANCE; 
    } 
    //loop through and store node description. Node counting starts at 1 to link up easier with 
    //the arcs. The NODESIZE has been increased by 1 to allow for this format. 
    for(int i=1;i<NODESIZE;i++) 
    { 
     getline(nodefile,Nodelist[i],'\t');      //store node data 
     nodefile.ignore(1000,'\n');        //ignore coordinates 
    } 

    //////////////////////Begin user interface portion of program//////////////////////////////// 

    cout<<"Arcs and Nodes loaded."<<endl; 
    cout<<"Please select by node number a starting node and ending node."<<endl; 
    cout<<"(Press any key to display the list)"<<endl<<endl; 
    getch(); 

    //print out the node list with numarical values 
    for(int i=1;i<NODESIZE;i++) 
    { 
     cout<<i<<" - "<<Nodelist[i]<<endl; 
    } 
    cout<<endl; 
    cout<<"Please select a starting node: "; 
    cin>>StartingNode; 
    cout<<endl<<"Please select an ending node: "; 
    cin>>EndingNode; 

    ////////////////////////SOME KIND OF ERROR OCCURS PAST THIS POINT/////////////////////// 

    //delete allocated memory 
    delete Arclist; 
    delete Nodelist; 


    getch(); 
} 

回答

2
Arclist = new Arc[ARCSIZE]; 
Nodelist = new string[NODESIZE]; 

delete Arclist; 
delete Nodelist; 

不匹配,which is an error

你的意思是:

delete[] Arclist; 
delete[] Nodelist; 

您的環境中檢測自己是不是分配和重新分配之間的匹配。

這實際上很幸運,因爲C++標準沒有強制任何診斷;在你的和其他平臺上,這個錯誤可能會默默「發揮作用」並導致各種內存損壞問題!

順便說一句,你main()功能必須回報int(儘管你可以離開了明確return 0;如果你喜歡,它會自動爲main(),如果你離開它完成)。

3

這些:

delete Arclist; 
delete Nodelist; 

應該是:

delete [] Arclist; 
delete [] Nodelist; 

或者更好的是,忘記使用動態分配的數組,並使用std::vector

+0

不幸的是,'的std :: VECTOR'不太可能被覆蓋/允許「基本編程類」。 – 2011-05-03 16:40:45

+1

@Tomalak嗯,當然應該。它比做自己的內存管理更基礎。 – 2011-05-03 16:44:36

+0

我們正在進入題外話題領域,但它足以說:「它應該是」的課程,而課程則不是。 :) – 2011-05-03 16:46:14

0

規則很簡單。如果您使用new來初始化數組,則必須使用delete[]語句將其刪除。

所以,你必須說

delete[] Arclist; 
delete[] Nodelist;