腐败的堆

时间:2012-06-21 14:55:50

标签: c++ visual-c++

在我的程序中,我遇到了“堆腐败”错误

“Windows在Project_Name.exe中触发了断点。 这可能是由于堆的损坏,这表明JPS.exe或它已加载的任何DLL中的错误。 这也可能是由于用户在Project_Name.exe具有焦点时按下F12。 输出窗口可能包含更多诊断信息。“

以下是代码:

Header.h

#pragma once

#include <math.h>
#include <iostream>
#include <fstream> 
#include <string>
#include <sstream>
#include <string>
#include <stdio.h>
#include <stack>
#include <vector>
#include <ctime>
#include <cassert>
#include <cmath>

using namespace std;

class ClientInfo
{
public:
int dayM;
string monthM;
int dayL;
string monthL;
string fName, lName;
double itmAmt;
ClientInfo* next;
};

class Record
{
private:
int numOfClients;
public:
int returnNumOfClients();
ClientInfo* head;
Record();
Record* InitializeRecord();
char* CalculateMaturity();
string ViewClient();
Record*  FindName(string nn, ClientInfo *xxx);
void SaveRecord(ClientInfo* h);
double ComputeBalance(double amount, string mnthL, int dyL, string mnthM, int dyM);
ClientInfo* AddClient(string fn,string ln,string xMonthL, int xDayL, string    
    xMonthM, int xDayM ,double xItemAmt);
ClientInfo* SearchClient(string n);
void RemoveItem(ClientInfo* h, ClientInfo* rc, string remfname);
bool CheckExistence(ClientInfo* hx, string fnx, string lnx);

template <typename T>
string numToStr ( T num )
{
    stringstream ss;
    ss << num;
    return ss.str();
}
};

Record::Record():numOfClients(0),head(NULL)
{
}

Record* Record::FindName(string nn, ClientInfo * xxx)
{
ClientInfo* newci = new ClientInfo();
Record* newrec = new Record();
ClientInfo* node= xxx;
do{
    if(node->lName==nn)
    {

        newrec->head=newrec->AddClient(node->fName, 
            node->lName, 
            node->monthL,
            node->dayL,
            node->monthM,
            node->dayM,node->itmAmt);

    }
    node=node->next;
}while(node!=NULL);

return newrec;
}

int Record::returnNumOfClients()
{
return numOfClients;
}

void Record::SaveRecord(ClientInfo* h)
{
Record *rec = new Record();
ClientInfo* saveRec = h;
string str;

vector<string> v;
ofstream outF;
outF.open("Record.txt");

if(saveRec!=NULL)
{
    do
    {
        str = saveRec->fName+'-'+saveRec->lName+'-'+
            rec->numToStr(saveRec->monthL)+'-'+
            rec->numToStr(saveRec->dayL)+'-'+
            rec->numToStr(saveRec->monthM)+'-'+
            rec->numToStr(saveRec->dayM)+'-'+
            rec->numToStr(saveRec->itmAmt);

        saveRec = saveRec->next;

        v.push_back(str);
    }
    while(saveRec != NULL);


    for(vector<string>::iterator it = v.begin(); it != v.end(); ++it)
    {
        if( it != v.begin())
            outF<<'\n';
        outF<< *it;
    }
}
else
    numOfClients=0;

outF.close();

}

char* Record::CalculateMaturity()
{
try
{
    ClientInfo* saveRec = new ClientInfo();
    Record* rec = new Record();
    string str;

    stack<string> month;
    month.push("December");
    month.push("November");
    month.push("October");
    month.push("September");
    month.push("August");
    month.push("July");
    month.push("June");
    month.push("May");
    month.push("April");
    month.push("March");
    month.push("February");
    month.push("January");
    month.push("December");
    month.push("November");
    month.push("October");
    month.push("September");
    month.push("August");
    month.push("July");
    month.push("June");
    month.push("May");
    month.push("April");
    month.push("March");
    month.push("February");
    month.push("January");

    stack<string> mnth;
    mnth.push("December");
    mnth.push("November");
    mnth.push("October");
    mnth.push("September");
    mnth.push("August");
    mnth.push("July");
    mnth.push("June");
    mnth.push("May");
    mnth.push("April");
    mnth.push("March");
    mnth.push("February");
    mnth.push("January");
    mnth.push("December");
    mnth.push("November");
    mnth.push("October");
    mnth.push("September");
    mnth.push("August");
    mnth.push("July");
    mnth.push("June");
    mnth.push("May");
    mnth.push("April");
    mnth.push("March");
    mnth.push("February");
    mnth.push("January");

    const char *delimeter="-";
    char* date=new char;


    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf), "%m-%d", &tstruct);

    date = buf;


    char* token= new char;
    vector<string> dt;

    token=strtok(date,delimeter);
    while(token!=NULL)
    {
        dt.push_back(token);
        token=strtok(NULL,delimeter);
    }

    string monthL,monthM;
    int dayL;
    int monthnum;
    istringstream(dt[0])>>monthnum;
    for(int x=1;x!=monthnum;x++)
        mnth.pop();
    monthL=mnth.top();


    istringstream (dt[1])>>dayL;

    do{month.pop();}while(monthL!=month.top());

    month.pop();
    month.pop();
    month.pop();

    monthM=month.top();

    str=monthL+'-'+
        rec->numToStr(dayL)+'-'+
        monthM+'-'+
        rec->numToStr(dayL);

    int strsize = str.size();
    char* chr = new char;

    for(int x=0;x<strsize;x++)
    {
        chr[x]=str[x];
    }
    return chr;
}
catch(char* ex){system("pause");}
}

ClientInfo* Record::AddClient(string fn, string ln,string xMonthL, int xDayL, string    
xMonthM, int xDayM, double xItemAmt)
{
ClientInfo* newInfo = new ClientInfo();
Record* newRecord = new Record();

newInfo->fName = fn;
newInfo->lName = ln;
newInfo->dayM = xDayM;
newInfo->monthM = xMonthM;
newInfo->dayL = xDayL;
newInfo->monthL = xMonthL;
newInfo->itmAmt = xItemAmt;

if(newInfo->next!=NULL)
{
    newInfo->next = head;
    numOfClients++;
    head = newInfo;
}
else
{
    newInfo->next=NULL;
}

return head;    
}

Record* Record::InitializeRecord()
{
Record* initRec = new Record();
ClientInfo* initClient = new ClientInfo();

ifstream inF;
inF.open("Record.txt");

char* rec;
char* token;
vector<string> v;
int dayL, dayM;
string monthL, monthM;
double itmAmt;

if(inF.good())
{
    do
    {
        rec = new char;
        token = new char;
        inF>>rec;
        token = strtok(rec,"-");
        while(token!=NULL)
        {
            v.push_back(token);
            token = strtok(NULL,"-");
        }

        istringstream(v[2])>>monthL;
        istringstream(v[3])>>dayL;
        istringstream(v[4])>>monthM;
        istringstream(v[5])>>dayM;
        istringstream(v[6])>>itmAmt;
        initRec->AddClient(v[0],v[1],monthL,dayL,monthM,dayM,itmAmt);
        v.erase(v.begin(),v.end());
    }while(!inF.eof());
}

inF.close();
return initRec;
}

void Record::RemoveItem(ClientInfo* h, ClientInfo* rc, string remfname)
{
ClientInfo* head = h;
ClientInfo* temp = rc->next;
if(temp!=NULL)
{
    rc->fName = rc->next->fName;
    rc->lName = rc->next->lName;
    rc->monthL = rc->next->monthL;
    rc->dayL = rc->next->dayL;
    rc->monthM = rc->next->monthM;
    rc->dayM = rc->next->dayM;
    rc->itmAmt = rc->next->itmAmt;

    rc->next = temp->next;
    numOfClients--;
    free(temp);
}
else
{
    head = NULL;
    free(temp);
}
SaveRecord(head);
}

ClientInfo* Record::SearchClient(string n)
{
ClientInfo* current = head;

for(int x=numOfClients-1;current!=NULL;x--)
{
    if(current->lName == n)
        return current;
    else
        current = current->next;
}

return current;
}

string Record::ViewClient()
{
string s;
ClientInfo* itr;
itr = head;
if(numOfClients!=0)
{
    for(int i = 0; i<returnNumOfClients();i++)
    {
        string empty = "";
        int y = i+1;
        s+="\n\n***************\nName: "+itr->fName+" "+itr->lName+"\n-----            
                    ----------\n"+
            "Loan Origination Date: "+numToStr(itr->monthL)+" 
                    "+numToStr(itr->dayL)+
            "\n---------------\n"+"Date Of Maturity: "+numToStr(itr-                       
                    >monthM)+" "+numToStr(itr->dayM)+"\n---------------\n"+
            "Balance: "+numToStr(itr->itmAmt);
        itr = itr->next;
    }

    s.append("\n***************\n\n");
}
else
    s ="There is no recorded client yet.\n\n";

return s;
}

double Record::ComputeBalance(double amount, string mnthL, int dyL, string mnthM, int  
    dyM)
{
try
{
    double INTEREST = 0.04;

    double PENALTY = 0.02;

    stack<string> monthl;
    monthl.push("December");
    monthl.push("November");
    monthl.push("October");
    monthl.push("September");
    monthl.push("August");
    monthl.push("July");
    monthl.push("June");
    monthl.push("May");
    monthl.push("April");
    monthl.push("March");
    monthl.push("February");
    monthl.push("January");
    monthl.push("December");
    monthl.push("November");
    monthl.push("October");
    monthl.push("September");
    monthl.push("August");
    monthl.push("July");
    monthl.push("June");
    monthl.push("May");
    monthl.push("April");
    monthl.push("March");
    monthl.push("February");
    monthl.push("January");


    stack<string> monthm;
    monthm.push("December");
    monthm.push("November");
    monthm.push("October");
    monthm.push("September");
    monthm.push("August");
    monthm.push("July");
    monthm.push("June");
    monthm.push("May");
    monthm.push("April");
    monthm.push("March");
    monthm.push("February");
    monthm.push("January");
    monthm.push("December");
    monthm.push("November");
    monthm.push("October");
    monthm.push("September");
    monthm.push("August");
    monthm.push("July");
    monthm.push("June");
    monthm.push("May");
    monthm.push("April");
    monthm.push("March");
    monthm.push("February");
    monthm.push("January");

    int ml=0;
    int mm=0;

    do{
        monthl.pop();
        mm++;
    }while(mnthL!=monthl.top());

    do
    {
        monthm.pop();
        ml++;
    }while(mnthM!=monthm.top());

    int diff=abs(mm-ml);
    double balance=diff*(amount*(INTEREST+  
                                                 (INTEREST/(pow(INTEREST+1,diff)-1))));

    return balance;
}
catch(double ex){system("pause");}
}

bool Record::CheckExistence(ClientInfo* hx, string fnx, string lnx)
{
ClientInfo *hh;
hh = head;
int t=0;

for(int x=numOfClients; hh!=NULL && x!=0; x--)
{
    if(hh->fName == fnx && hh->lName==lnx)
        t++;
    else
        hh=hh->next;
}

if(t!=0)
    return true;
else
    return false;
}

Main.cpp的

#include "Header.h"

int main()
{

Record* rec = new Record();
ClientInfo* info;

Record* samelastrec = new Record();
double amt;
double balance;
int number;
int choose;
char ch;
double principal;
string iLName,iFName;
string nn;
int iDayL,iDayM;
string iMonthL,iMonthM;
string delfname;
rec=rec->InitializeRecord();

again:
system("cls");
cout<<"WELCOME TO JEWELRY PAWNING SYSTEM."<<endl;
cout<<"\n(1)Add Client\n(2)Remove Client\n(3)Search Client\n\n->";

cin>>choose;
Record *find = new Record();
ClientInfo* found;


system("cls");
int search;
char* datelm;
char* token;
vector<string> date;
string nnx; 
switch(choose)
{
case 1:

    system("cls");

    if(rec->returnNumOfClients()!=0 || rec->returnNumOfClients()!=NULL)
    {
        cout<<"First Name: ";
        cin>>iFName;
        cout<<"Last name: ";
        cin>>iLName;

        if(!rec->CheckExistence(rec->head,iFName,iLName))
        {
            cout<<"Amount: ";
            cin>>amt;
            datelm=rec->CalculateMaturity();

            token=strtok(datelm, "-");
            while(token!=NULL)
            {
                date.push_back(token);
                token=strtok(NULL, "-");
            }

            istringstream(date[0])>>iMonthL;
            istringstream(date[1])>>iDayL;
            istringstream(date[2])>>iMonthM;
            istringstream(date[3])>>iDayM;

    balance=rec->ComputeBalance(amt,iMonthL,iDayL,iMonthM,iDayM);
            istringstream(rec->numToStr(balance));

    rec->head=rec>AddClient(iFName,iLName,iMonthL,iDayL,iMonthM,iDayM,balance);
            system("pause");
        }
        else
        {
            cout<<"Member already exist!"<<endl;
            system("pause");
        }
    }
    else
    {
        cout<<"First Name: ";
        cin>>iFName;
        cout<<"Last name: ";
        cin>>iLName;
        cout<<"Amount: ";
        cin>>amt;
        datelm=rec->CalculateMaturity();

        token=strtok(datelm, "-");
        while(token!=NULL)
        {
            date.push_back(token);
            token=strtok(NULL, "-");
        }

        istringstream(date[0])>>iMonthL;
        istringstream(date[1])>>iDayL;
        istringstream(date[2])>>iMonthM;
        istringstream(date[3])>>iDayM;

        balance=rec->ComputeBalance(amt,iMonthL,iDayL,iMonthM,iDayM);
        istringstream(rec->numToStr(balance));

    rec->head=rec>AddClient(iFName,iLName,iMonthL,iDayL,iMonthM,iDayM,balance);
        system("pause");
    }

    rec->SaveRecord(rec->head);
    goto again;
    break;

case 2:
    system("cls");
    find->head = rec->head;
    cout<<"Enter Last Name: ";
    cin>>iLName;
    samelastrec=new Record();
    samelastrec=samelastrec->FindName(iLName, rec->head);


    if(find->SearchClient(iLName) == NULL)
    {
        cout<<"Client not found.\n";
        system("pause");
    }
    else
    {

        found = find->SearchClient(iLName);

        if(samelastrec->returnNumOfClients()>1)
        {
            cout<<samelastrec->ViewClient()<<endl<<endl;


           cout<<"There are "<<samelastrec->numToStr(samelastrec->returnNumOfClients())    
                            <<" Clients with "<<samelastrec->head->lName<<" last name";
            cout<<endl<<"Enter the first name of the client you want to 
                                                                             remove: ";
            cin>>delfname;
            system("cls");

            if(samelastrec->CheckExistence(samelastrec->head,delfname, 
                                                                            iLName))
            {
                cout<<endl<<"Do you want to remove "<<delfname<<" 
                                            <<samelastrec->head->lName<<" ?(Y/N)";
                cin>>ch;

                if(ch=='Y' || ch=='y')
                {
                    rec->RemoveItem(rec->head,found,delfname);
                    cout<<"Client Removed."<<endl;
                    system("cls");

                }
                else if(ch=='N' || ch=='n')
                    goto again;
            }
            else
            {
                cout<<"The member with "<<delfname<<" does not 
                                    exist."<<endl;
                system("pause");
                goto again;
            }

            system("pause");
        }
        else
        {
            cout<<endl<<samelastrec->ViewClient()<<endl;
            cout<<"Remove Client? [Y/N]: ";
            cin>>ch;

            if(ch=='Y' || ch=='y')
            {
                system("cls");
                rec->RemoveItem(rec->head,found,found->fName);
                cout<<"Client Removed."<<endl;
                system("pause");
            }
            else if(ch=='N' || ch=='n')
                goto again;
        }
    }
    goto again;
    break;

case 3:
    system("cls");
    cout<<"Enter Last Name : ";
    cin>>nnx;

    samelastrec=new Record();
    samelastrec=samelastrec->FindName(nnx, rec->head);
    cout<<samelastrec->ViewClient()<<endl;


    system("pause");
    break;
case 4:
    system("cls");
    cout<<rec->ViewClient();
    system("pause");
    goto again;
    break;

case 5:
    cout<<"Number of clients: "<<rec->numToStr(rec->returnNumOfClients())
                                                                                <<endl;
    system("pause");
    break;
default:
    if(choose!=0)
    {
        cout<<"\n\nInvalid Entry.\n\n";
        system("pause");
        goto again;
    }
    else
    {
        cout<<"\n\nTHANK YOU FOR USING THE JEWELRY PAWNING SYSTEM!\n\n";
        system("pause");
        exit(1);
    }
}
goto again;
system("pause");
return 0;
}

我无法弄清楚为什么我会收到这些错误。

请帮我调试此错误。

谢谢!

1 个答案:

答案 0 :(得分:3)

在整个代码中,您有类似这样的部分,这些部分会丢弃您不拥有的内存:

int strsize = str.size();
char* chr = new char;

for(int x=0;x<strsize;x++)
{
    chr[x]=str[x];
}
return chr;

看起来你不太明白指针和new是如何工作的。 new char;为一个字符分配足够的内存 - 通常是一个字节;因此,当x在后​​续循环中大于0时,您将覆盖您不拥有的内存,最终导致崩溃。你有很多这样的代码部分。为操作分配正确的内存量(例如new char[strsize]),或者(最好)使用C ++字符串而不是字符数组。

(我还注意到你似乎没有释放你分配的任何内存,这最终会导致问题,因为你在程序运行期间执行更多的分配。通常,new的每次使用都应该匹配相应使用delete)。