数据结构的一个简单习题,但vc6,vc7,vc8均出错了,不知道是怎么回事???

dongweizi 2011-01-11 07:49:32
问题如下:

1,VC6 下,debug下单步看值都正确,但把断点都去掉,直接执行y的值不正确,但如果交换x与y的行,则结果正确,即如下是没问题的
:
int main(int argc, char* argv[])
{
BDStacktype dstack;



Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int y = 99;
pop(dstack,1,y);
/*cout<<"the high stack is "<<x<<endl;*/
if (y>3)
{
cout<<"md"<<endl;
}
cout<<y<<endl;

int x = 99;
pop(dstack,0,x);
cout<<"the low stack is "<<x<<endl;



return 0;
}
2,VC7和VC8均是编译OK但执行cout地,提示unhandled exception user breakpoint云云,不知道是怎么回事,请教大侠们,给个明白哈,谢先!!

3,出问题的完整代码如下:


#include "stdafx.h"
#include <stdio.h>


#include "iostream"
using namespace std;
#include "string.h"
#include "stdlib.h"
#include "ctype.h"

#define RT_Success 0
#define RT_Failed 1
#define RT_NotImpl 2
#define Status int

typedef struct{
int *base[2];
int *top[2];
}BDStacktype; //双向栈类型

Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0] = new int;
tws.base[1] = tws.base[0] + m;

tws.top[0] = tws.base[0];
tws.top[1] = tws.base[1];

return RT_Success;

}

Status push(BDStacktype &tws,int i,int x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if(tws.top[0]>tws.top[1])
return RT_Failed;
switch(i)
{
case 0:
*(tws.top[0]) = x;
tws.top[0]++;
break;
case 1:
*(tws.top[1]) = x;
tws.top[1] --;
break;
default:
return RT_Failed;
}
return RT_Success;

}

Status pop(BDStacktype &tws,int i,int &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
switch(i)
{
case 0:
if (tws.base[0] == tws.top[0])
{
return RT_Failed;
}
tws.top[0]--;
x = *(tws.top[0]);
break;
case 1:
if (tws.base[1] == tws.top[1])
{
return RT_Failed;
}
tws.top[1] ++;
x = *(tws.top[1]);
break;
default:
return RT_Failed;
}
return RT_Success;

}

void _tmain(int argc, _TCHAR* argv[])
{

//return 0;
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int x = 99;
pop(dstack,0,x);
std::cout << "the low stack is " << x << endl;

//int y = 99;
//pop(dstack,1,y);
//std::cout<<"the high stack is "<<x<<endl;
//if (y>3)
//{
// std::cout<<"md"<<endl;
//}
//std::cout<<y<<endl;


}
...全文
157 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongweizi 2011-01-20
  • 打赏
  • 举报
回复
谢谢各位,学习了,先结贴,再次感谢!
pstrunner 2011-01-12
  • 打赏
  • 举报
回复
这样测试一下,就更加明白啦。

BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int x = 99;
//pop(dstack,0,x);
//x = 1;
//std::cout << "the low stack is " << x << endl;
std::cout << "the low stack is "<< endl;
pstrunner 2011-01-12
  • 打赏
  • 举报
回复
因为6楼已经指出你有越界的操作啦,致使内存被污染啦。
在调用(std::cout << "the low stack is " << x << endl;)这句话时,当输出后会有内存释放的,而这时就会Dump啦。
不信的话,你可以如下测试一下,一样也会dump的。

int x = 99;
//pop(dstack,0,x);
x = 1;
std::cout << "the low stack is " << x << endl;

[Quote=引用 8 楼 dongweizi 的回复:]
logiciel,你改的真是很对,但是我想知道原因哈?为什么出错?为什么原程序中将xy的位置换下就没问题呢,即改成下面这样OK,非常感谢先!:)
int main(int argc, char* argv[])
{
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int ……
[/Quote]
na2650945 2011-01-12
  • 打赏
  • 举报
回复
代码的指针有问题。
和编译器有关系么?
dongweizi 2011-01-12
  • 打赏
  • 举报
回复
logiciel,你改的真是很对,但是我想知道原因哈?为什么出错?为什么原程序中将xy的位置换下就没问题呢,即改成下面这样OK,非常感谢先!:)
int main(int argc, char* argv[])
{
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int y = 99;
pop(dstack,1,y);
cout<<"the high stack is "<<y<<endl;
if (y>3)
{
cout<<"md"<<endl;
}
cout<<y<<endl;
int x = 99;
pop(dstack,0,x);
cout<<"the low stack is "<<x<<endl;
return 0;
}
logiciel 2011-01-12
  • 打赏
  • 举报
回复
tws.base[0] = new int;
改为
tws.base[0] = new int[m];
走好每一步 2011-01-12
  • 打赏
  • 举报
回复
当时我就是这样,调试的时候好好的,但一运行程序崩溃。当时还赶进度,那个错误花了我一天时间…
走好每一步 2011-01-12
  • 打赏
  • 举报
回复
内存越界,这种错误相当难找,而且后果不用我说吧。
logiciel 2011-01-12
  • 打赏
  • 举报
回复
同意10楼.以下代码也可显示为何被改为512:

int x = 99;
pop(dstack,0,x);
printf("dstack.top[1]+1=%x *(dstack.top[1]+1): %d\n", dstack.top[1]+1, *(dstack.top[1]+1));
cout<<"the low stack is "<<x<<endl;
printf("dstack.top[1]+1=%x *(dstack.top[1]+1): %d\n", dstack.top[1]+1, *(dstack.top[1]+1));
上面的cout修改了*(dstack.top[1]+1).

输出:
dstack.top[1]+1=381128 *(dstack.top[1]+1): 2
the low stack is 1
dstack.top[1]+1=381128 *(dstack.top[1]+1): 512

另外:
tws.base[0] = new int;
应改为
tws.base[0] = new int[m+1];
这是因为在push(dstack,1,2)时用的是tws.base[0] + m.
arong1234 2011-01-11
  • 打赏
  • 举报
回复
说明lz哪里有bug
dongweizi 2011-01-11
  • 打赏
  • 举报
回复
我试了你的code但结果和楼主的一样,还是y值不对,y打出来是512,其实应该是2,我不明白vc6这个y为什么打不对,vc7和vc8下程序崩溃..

#include "stdafx.h"
#include <stdio.h>


#include "iostream.h"
#include "string.h"
#include "stdlib.h"
#include "ctype.h"

#define RT_Success 0
#define RT_Failed 1
#define RT_NotImpl 2
#define Status int

typedef struct{
int *base[2];
int *top[2];
}BDStacktype; //双向栈类型

Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0] = new int;
tws.base[1] = tws.base[0] + m;

tws.top[0] = tws.base[0];
tws.top[1] = tws.base[1];

return RT_Success;

}

Status push(BDStacktype &tws,int i,int x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if(tws.top[0]>tws.top[1])
return RT_Failed;
switch(i)
{
case 0:
*(tws.top[0]) = x;
tws.top[0]++;
break;
case 1:
*(tws.top[1]) = x;
tws.top[1] --;
break;
default:
return RT_Failed;
}
return RT_Success;

}

Status pop(BDStacktype &tws,int i,int &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
switch(i)
{
case 0:
if (tws.base[0] == tws.top[0])
{
return RT_Failed;
}
tws.top[0]--;
x = *(tws.top[0]);
break;
case 1:
if (tws.base[1] == tws.top[1])
{
return RT_Failed;
}
tws.top[1] ++;
x = *(tws.top[1]);
break;
default:
return RT_Failed;
}
return RT_Success;

}

void main()
{

//return 0;
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int x = 99;
pop(dstack,0,x);
cout << "the low stack is " << x << endl;

int y = 99;
pop(dstack,1,y);

if (y>3)
{
cout<<"md"<<endl;
}
cout<<y<<endl;


}
無_1024 2011-01-11
  • 打赏
  • 举报
回复

//#include "stdafx.h"
#include <stdio.h>


#include "iostream"
using namespace std;
#include "string.h"
#include "stdlib.h"
#include "ctype.h"

#define RT_Success 0
#define RT_Failed 1
#define RT_NotImpl 2
#define Status int

typedef struct{
int *base[2];
int *top[2];
}BDStacktype; //双向栈类型

Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0] = new int;
tws.base[1] = tws.base[0] + m;

tws.top[0] = tws.base[0];
tws.top[1] = tws.base[1];

return RT_Success;

}

Status push(BDStacktype &tws,int i,int x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if(tws.top[0]>tws.top[1])
return RT_Failed;
switch(i)
{
case 0:
*(tws.top[0]) = x;
tws.top[0]++;
break;
case 1:
*(tws.top[1]) = x;
tws.top[1] --;
break;
default:
return RT_Failed;
}
return RT_Success;

}

Status pop(BDStacktype &tws,int i,int &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
switch(i)
{
case 0:
if (tws.base[0] == tws.top[0])
{
return RT_Failed;
}
tws.top[0]--;
x = *(tws.top[0]);
break;
case 1:
if (tws.base[1] == tws.top[1])
{
return RT_Failed;
}
tws.top[1] ++;
x = *(tws.top[1]);
break;
default:
return RT_Failed;
}
return RT_Success;

}

void main()
{

//return 0;
BDStacktype dstack;
Init_Stack(dstack,10);
push(dstack,0,1);
push(dstack,1,2);

int x = 99;
pop(dstack,0,x);
std::cout << "the low stack is " << x << endl;

//int y = 99;
//pop(dstack,1,y);
//std::cout<<"the high stack is "<<x<<endl;
//if (y>3)
//{
// std::cout<<"md"<<endl;
//}
//std::cout<<y<<endl;


}
//这样在我的VC6.0可以运行

64,282

社区成员

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

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