下面这段代码存在着内存泄漏,大家看看怎样释放
/*****************************************************************************************
**                                     桶排序算法
******************************************************************************************/#include <afx.h>
#include <time.h>
#include <iostream>
#include <windows.h>using namespace std;const int COUNT = 100000;
const int BUCKETCOUNT = 32767 / 100 + 1;struct node
{
int item;
node *next;
node(int x, node *n)
{
item = x;
next = n;
}
};typedef node * list;void BucketSort(int array[])
{
list Bucket[BUCKETCOUNT];
for(int i=0; i<BUCKETCOUNT; i++)
{
Bucket[i] = new node(0, NULL);
}

for(i=0; i<COUNT; i++)
{
int pos = array[i] / 100;
list newnode = new node(array[i], NULL);
if(Bucket[pos]->next == NULL)
Bucket[pos]->next = newnode;
else
{
node *q = Bucket[pos];
node *p = q->next;
while(p != NULL)
{
if(p->item <= array[i])
{
q = p;
p = p->next;
}
else
{
q->next = newnode;
newnode->next = p;
break;
}
}
if(p == NULL)
q->next = newnode;
}
} int n=0;
for(i=0; i<BUCKETCOUNT; i++)
{
node *p = Bucket[i];
if(p->next != NULL)
{
p = p->next;
while(p != NULL)
{
array[n] = p->item;
n++;
p = p->next;
}
}
delete []Bucket[i];
}

}void main()
{
char temp[10];
int array[COUNT];
CStdioFile file;
//产生原始数据
file.Open("original.txt", CFile::modeWrite | CFile::modeCreate | CFile::typeText);
srand((unsigned)time(NULL));
for(int i=0; i<COUNT; i++)
{
array[i] = rand();
sprintf(temp, "%d", array[i]);
file.Write(temp, strlen(temp));
file.Write("\n", 1);
}
file.Close();
//进行插入排序
DWORD dwStart, dwStop; dwStart = GetTickCount();
BucketSort(array);
dwStop = GetTickCount();
cout << "time used: " << dwStop-dwStart << "ms" << endl;
//输出排序结果
file.Open("sorted.txt", CFile::modeWrite | CFile::modeCreate | CFile::typeText);
for(i=0; i<COUNT; i++)
{
sprintf(temp, "%d", array[i]);
file.Write(temp, strlen(temp));
file.Write("\n", 1);
}
file.Close();

}

解决方案 »

  1.   

    new 和 delete配对就行。
      

  2.   

    delete []Bucket[i];==>改为delete Bucket[i];list newnode = new node(array[i], NULL);==〉使用之后就要用delete newnode;
      

  3.   

    因为newnode是连接到链表中,所以我使用了下面的语句后就会出现异常中断
    list newnode = new node(array[i], NULL);==〉使用之后就要用delete newnode;
      

  4.   

    呵呵,问题终于解决了,链表中的节点不能 new 后马上 delete 要在用完后进行链表的遍历并释放内存,修改后的代码如下:/*****************************************************************************************
    **                                     桶排序算法
    ******************************************************************************************/#include <afx.h>
    #include <time.h>
    #include <iostream>
    #include <windows.h>using namespace std;const int COUNT = 100000;
    const int BUCKETCOUNT = 32767 / 100 + 1;struct node
    {
    int item;
    node *next;
    node(int x, node *n)
    {
    item = x;
    next = n;
    }
    };typedef node * list;void BucketSort(int array[])
    {
    list Bucket[BUCKETCOUNT];
    for(int i=0; i<BUCKETCOUNT; i++)
    {
    Bucket[i] = new node(0, NULL);
    }

    for(i=0; i<COUNT; i++)
    {
    int pos = array[i] / 100;
    list newnode = new node(array[i], NULL);
    if(Bucket[pos]->next == NULL)
    Bucket[pos]->next = newnode;
    else
    {
    node *q = Bucket[pos];
    node *p = q->next;
    while(p != NULL)
    {
    if(p->item <= array[i])
    {
    q = p;
    p = p->next;
    }
    else
    {
    q->next = newnode;
    newnode->next = p;
    break;
    }
    }
    if(p == NULL)
    q->next = newnode;
    }
    } int n=0;
    for(i=0; i<BUCKETCOUNT; i++)
    {
    node *p = Bucket[i];
    node *q = p;
    if(p->next != NULL)
    {
    p = p->next;
    delete q;
    while(p != NULL)
    {
    array[n] = p->item;
    n++;
    q = p;
    p = p->next;
    delete q;
    }
    }
    else
    delete p;
    }}void main()
    {
    char temp[10];
    int array[COUNT];
    CStdioFile file;
    //产生原始数据
    file.Open("original.txt", CFile::modeWrite | CFile::modeCreate | CFile::typeText);
    srand((unsigned)time(NULL));
    for(int i=0; i<COUNT; i++)
    {
    array[i] = rand();
    sprintf(temp, "%d", array[i]);
    file.Write(temp, strlen(temp));
    file.Write("\n", 1);
    }
    file.Close();
    //进行插入排序
    DWORD dwStart, dwStop; dwStart = GetTickCount();
    BucketSort(array);
    dwStop = GetTickCount();
    cout << "time used: " << dwStop-dwStart << "ms" << endl;
    //输出排序结果
    file.Open("sorted.txt", CFile::modeWrite | CFile::modeCreate | CFile::typeText);
    for(i=0; i<COUNT; i++)
    {
    sprintf(temp, "%d", array[i]);
    file.Write(temp, strlen(temp));
    file.Write("\n", 1);
    }
    file.Close();

    }