完整的代码如下,但是释放内存出错的地方在代码的最下面。。希望高手解释一下出错的原因,感激不尽。。
#include <iostream>
using namespace std ;
int main()
{
void Conflate(int num[], int count, int res[], int base) ;
void EndConflate(int num[], int count) ;
int m_count ;
cin >> m_count ;
int *m_num = new int[m_count] ;
if(NULL == m_num)
{
cout << "内存分配错误!" << endl ;
exit(0) ;
}
for(int i=0; i<m_count; i++)
{
cin >> m_num[i] ;
}
EndConflate(m_num, m_count) ;
int m = 0 ;
for(i=0; i<m_count; i++, m++)
{
if(10 == m)
{
cout << endl ;
m = 0 ;
}
cout << m_num[i] << " " ;
}
delete []m_num ;
return 0 ;
}
void Conflate(int num[], int count, int res[], int base)
{
int up_num_f ; //第一个子数组的上界
int low_num_f = 0 ; //第一个子数组的下界
int up_num_b ; //第二个子数组的上界
int low_num_b ; //第二个子数组的下界
int pos = 0 ;
while(low_num_f+base <= count-1)
{
low_num_b = low_num_f + base ;
up_num_f = low_num_b - 1 ;
up_num_b = (low_num_b < count) ? (low_num_b+base-1) : (count-1) ;
for(int i=low_num_f, j=low_num_b; (i<=up_num_f && j<=up_num_b); pos++)
{
if(num[i] <= num[j])
{
res[pos] = num[i] ;
i++ ;
}
else
{
res[pos] = num[j] ;
j++ ;
}
}
while(i <= up_num_f)
{
res[pos] = num[i] ;
pos++ ;
i++ ;
}
while(j <= up_num_b)
{
res[pos] = num[j] ;
pos++ ;
j++ ;
}
low_num_f = up_num_b + 1 ;
}
for(int i=low_num_f; i<count; i++, pos++)
{
res[pos] = num[i] ;
}
}
void EndConflate(int num[], int count)
{
int base = 1 ;
int *temp = new int[count] ;
if(NULL == temp)
{
cout << "内存分配错误!" << endl ;
exit(0) ;
}
while(base < count)
{
Conflate(num, count, temp, base) ;
for(int i=0; i<count; i++)
{
num[i] = temp[i] ;
}
base = base * 2 ;
}
delete []temp ; //////////////////////////这里会出错,为什么呀??高手救命啊!!
}
希望高手解释一下出错的原因,感激不尽。。
[此贴子已经被作者于2007-8-21 16:04:37编辑过]