奇怪的结果:同一段程序在不同编译器上的结果(codeblock,vc6.0和vs2008)

march_on 2010-04-18 09:23:25

先贴代码,实现的是合并排序,比较简单
/******************************************************************
*使用方法:
*1.输入待排序的数组长度
*2.依次输入数组元素值
*3.无错则输出排序后的数组
*备注:
*1.使用模板技术,数组成员必须支持<,<=运算,否则程序无法正常运行
*2.运行程序时遇到了比较奇怪的地方,无论DEBUG宏是否定义,条件编译
* 语句均会执行
******************************************************************/
#include <iostream>
#include <stdlib.h>//for function exit(int);
#include <stdexcept>
#define DEBUG
using namespace std;

template <typename T>
void merge_sort(T array[],size_t begin_index,size_t end_index,size_t size);
template<typename T>
void merge(T array[],size_t begin_index,size_t middle_index,size_t end_index);

int main()
{
size_t size;
cout<<"please input the size of the array"<<endl;
cin>>size;
int*array;
try
{
array=new int[size];
}catch(const bad_alloc& ba)
{
cerr<<"failed to allocate memory"<<endl;
exit(1) ;
}

size_t index;
cout<<"please input the elements in the array"<<endl;
for(index=0;index!=size;++index)
cin>>array[index];
try
{
merge_sort(array,0,size-1,size);
}catch(const invalid_argument& ia)
{
ia.what();
exit(1);
}catch(const bad_alloc& ba)
{
cerr<<"failed to allocate memory "<<endl;
exit(1);
}
for(index=0;index!=size;++index)
cout<<array[index]<<" ";
delete[]array;
return 0;
}

/****************************************************************************
*函数名:merge_sort
*功能:实现合并排序
*参数:array指向待排序的数组
* bi和ei分别为数组第一个和最后一个元素的下标
* size为数组长度
*返回值:无
**************************************************************************/
template<typename T> void merge_sort(T array[],size_t bi,size_t ei,size_t size)
{
size_t index;
int mi;
if(bi<ei)
{
mi=(bi+ei)/2;
merge_sort(array,bi,mi,mi-bi+1);
merge_sort(array,mi+1,ei,ei-mi);
merge(array,bi,mi,ei);
#ifndef DEBUG
for(index=0;index!=size;++index)
cout<<array[index]<<"\t";
cout<<endl;
#endif
}
}

/**********************************************************************
*函数名:merge
*功能:合并排序的子过程,完成两个已排序数组的合并
*参数:array,bi,ei含义同merge_sort
* mi为前一个数组的末尾元素下标,亦即后一个数组的首个元素下标
*返回值:无
*********************************************************************/
template<typename T> void merge(T array[],size_t bi,size_t mi,size_t ei)
{
int index1,index2;
int size1=mi-bi+1;//第一个数组的长度
int size2=ei-mi;//第二个数组的长度
int bi2=bi;
T* array1=new T[size1+1];
T* array2=new T[size2+1];
for(index1=0;index1!=size1;++index1)
{
array1[index1]=array[bi2++];//因为bi要在后面用到,所以此处创建一个临时对象bi2
#ifndef DEBUG
cout<<array1[index1]<<"\t";
#endif
}

cout<<endl;
for(index2=0;index2!=size2;++index2)
{

array2[index2]=array[++mi];
#ifndef DEBUG
cout<<array2[index2]<<"\t";
#endif
}
cout<<endl;
for(index1=0,index2=0;index1!=size1&&index2!=size2;)
{
if(array1[index1]<array2[index2])
{
array[bi++]=array1[index1++];
}
else
{
array[bi++]=array2[index2++];
}
}
if(index2==size2)
{
while(bi<=ei)
array[bi++]=array1[index1++];
}
else
while (bi<=ei)
array[bi++]=array2[index2++];

//使用下述方法时必须在数组末尾设置一个结束标志,如
//T为整型时,可以设为无穷大
/*index1=0;
index2=0;
while(bi<=ei)
{
if(array1[index1]<array2[index2])
{
array[bi++]=array1[index1++];
if(index1==size1)

}
else
array[bi++]=array2[index2++];
}*/
delete[]array1;
delete[]array2;
}

程序实现合并排序,可是在不同的编译器中运行结果差别很大
在codeblock上面的问题是无论DEBUG宏有没有定义,条件编译那部分的代码都会执行
更奇怪的是在vc6.0上竟然连编译都不过,结果如下:
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(33) : warning C4101: 'ba' : unreferenced local variable
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(50) : warning C4101: 'ba' : unreferenced local variable
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(73) : error C2065: 'bi' : undeclared identifier
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(45) : see reference to function template instantiation 'void __cdecl merge_sort(int [],unsigned int,unsigned int,unsigned int)' being compiled
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(73) : error C2065: 'ei' : undeclared identifier
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(45) : see reference to function template instantiation 'void __cdecl merge_sort(int [],unsigned int,unsigned int,unsigned int)' being compiled
在vs2008上并没有什么奇怪的地方,运行结果正确,只是输入完数组后,结果要隔好几行之后才输出,不知道为什么
高手帮忙给看看啊
另外,程序有什么不妥的地方还请大家指点啊
...全文
207 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
march_on 2010-04-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 vmary 的回复:]
能不能简单明了点啊
[/Quote]
其实你不用仔细看代码,关键是运行的结果

1.在codeblock上面的问题是无论DEBUG宏有没有定义,条件编译那部分的代码都会执行
2.更奇怪的是在vc6.0上竟然连编译都不过,结果如下:
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(33) : warning C4101: 'ba' : unreferenced local variable
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(50) : warning C4101: 'ba' : unreferenced local variable
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(73) : error C2065: 'bi' : undeclared identifier
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(45) : see reference to function template instantiation 'void __cdecl merge_sort(int [],unsigned int,unsigned int,unsigned int)' being compiled
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(73) : error C2065: 'ei' : undeclared identifier
E:\Program Files\Microsoft Visual Studio\MyProjects\MergeSort\main.cpp(45) : see reference to function template instantiation 'void __cdecl merge_sort(int [],unsigned int,unsigned int,unsigned int)' being compiled
3.在vs2008上并没有什么奇怪的地方,运行结果正确,只是输入完数组后,结果要隔好几行之后才输出,不知道为什么
Vmary 2010-04-18
  • 打赏
  • 举报
回复
能不能简单明了点啊

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧