简体   繁体   English

调试断言在删除分配的 memory 时失败

[英]Debug Assertion Failed at deleted allocated memory

I'm in a relatively basic programming class and I'm getting an error I've never come across before.我正在进行一个相对基本的编程 class 并且我遇到了一个我以前从未遇到过的错误。 It's this:是这样的:

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)

Here's my code and the error pops up as the allocated memory is deleted it looks like这是我的代码,当分配的 memory 被删除时弹出错误,看起来像

    //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();
}

These:这些:

delete Arclist;
delete Nodelist;

should be:应该:

delete [] Arclist;
delete [] Nodelist;

Or better yet, forget using dynamically allocated arrays, and use std::vector .或者更好的是,忘记使用动态分配的 arrays,而使用std::vector

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

and

delete Arclist;
delete Nodelist;

do not match, which is an error .不匹配,这是一个错误

You mean:你的意思是:

delete[] Arclist;
delete[] Nodelist;

Your environment is detecting that something is not matching between the allocation and de-allocation.您的环境正在检测分配和取消分配之间的某些内容不匹配。

This is quite fortunate, actually, because the C++ Standard does not mandate any diagnostics;实际上,这非常幸运,因为 C++ 标准不要求进行任何诊断。 on yours and other platforms, this mistake could silently "work" and cause all manner of memory corruption issues!在您的平台和其他平台上,此错误可能会默默地“起作用”并导致各种 memory 损坏问题!

Incidentally, your main() function must return int (though you can leave out the explicit return 0; if you like; it'll be done automatically for main() if you leave it out).顺便说一句,您的main() function必须返回int (尽管您可以省略显式return 0;如果您愿意;如果您省略它,它将自动为main()完成)。

The rule is simple.规则很简单。 If you use new for initializing an array, you must delete it with the delete[] statement.如果使用new初始化数组,则必须使用delete[]语句将其删除。

So you must say所以你必须说

delete[] Arclist;
delete[] Nodelist;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM