恼人的问题又来了,这次我一定要解决,哪位大仙帮帮我。 搞定后,请发到我的油箱,请带一些文字叙述,讲讲原因。[email protected]不胜感激!!!因为我已经遇到了好几次这种发行版本出问题的怪事,但后来有的自己就好了,我也不知道为什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 对了,在Debug下调试结束时,好象有点问题,但是就是不知问题出在哪里? 我还是直接跟你说吧,因为很简单的:你的默认值:int CBigRect::WGrid = 3;int CBigRect::HGrid = 3;littlerect = new CLittleRect[WGrid*HGrid]; // 3*3大小但当你选择了4*4时你并没有重新分配内存,但WGrid*HGrid已经变成了4*4所以 引起了内存访问错误.修改方法:void CBigRect::InitAllRect(){//重新分配内存 if(littlerect != NULL){delete []littlerect;littlerect = new CLittleRect[WGrid*HGrid];} for(int i=0;i<WGrid;i++) for(int j=0;j<HGrid;j++) { littlerect[i*WGrid+j].index = i*WGrid+j; littlerect[i*WGrid+j].TYPE = 0; }} 我知道内存泄露也是出在这里。可是我在SetMode()之前用void CMyPTDlg::OnSethard3() { // TODO: Add your command handler code here if(MyBigRect.littlerect!=NULL) delete []MyBigRect.littlerect; MyBigRect.SetMode(3,3); OnNewgame();}这么用会有错误呢? 问题我明白了,我先只是删除了指针,但是没有重新new空间,谢谢你了dongfa(阿东)我以前也知道问题可能出在没有重新分配内存,只不过我的解决方法不是放在InitAllRect()函数中,你的办法可以解决问题,但是能帮我讲一下为什么没有重新分配程序还可以运行呢? 在debug下程序不是很紧凑,你使了其他内存空间,恰好那段内存也是可以访问的,所以有时候是可以运行的。release下程序比较紧凑,所以如果访问了未分配的内存一般都是出错的。不知道你有没有理解?我知道我的说法不是很专业. 开始,你定义int CBigRect::WGrid = 3;int CBigRect::HGrid = 3;并且初始化littlerect = new CLittleRect[WGrid*HGrid];也就是说littlerect的长度是9。setMode(4,4)后,WGrid和HGrid都是4。在InitAllRect中for(int i=0;i<WGrid;i++) for(int j=0;j<HGrid;j++) { littlerect[i*WGrid+j].index = i*WGrid+j; littlerect[i*WGrid+j].TYPE = 0; }当i=3,j=3时i*Wgrid+j = 11,littlerect[11]超过了你申请的内存。改为:int CBigRect::WGrid = 4;int CBigRect::HGrid = 4;一切ok。 同意dongfa(阿东)和leeshan(李山)是你的内存越界引起的. 小女子请教:如何实现类似winrar的文件目录浏览功能 关于数组的转换问题 把一个WebBrowser窗口里的内容都写到bmp里,有什么主意吗? 各位大侠,数据库编程奇怪问题高手请进 急,自行车运动声音? 分别在debug,release模式下运行有什么区别,那位大大能给我讲讲个子怎么回事么? CEit的滚动问题 帮助:那儿有非模态窗口的源码! NT or 2000,普通用户登陆,执行程序,想得到KEY_LOCALMACHINE的访问修改权限 请问用什么api能得到当前得到焦点的窗口的句柄? 10个线程同时发送 WM_USER+1000这个消息,执行......... (30分) 内存问题
你的默认值:
int CBigRect::WGrid = 3;
int CBigRect::HGrid = 3;
littlerect = new CLittleRect[WGrid*HGrid]; // 3*3大小
但当你选择了4*4时你并没有重新分配内存,但WGrid*HGrid已经变成了4*4
所以 引起了内存访问错误.
修改方法:
void CBigRect::InitAllRect()
{
//重新分配内存
if(littlerect != NULL){
delete []littlerect;
littlerect = new CLittleRect[WGrid*HGrid];
}
for(int i=0;i<WGrid;i++)
for(int j=0;j<HGrid;j++)
{
littlerect[i*WGrid+j].index = i*WGrid+j;
littlerect[i*WGrid+j].TYPE = 0;
}}
可是我在
SetMode()之前用
void CMyPTDlg::OnSethard3()
{
// TODO: Add your command handler code here
if(MyBigRect.littlerect!=NULL)
delete []MyBigRect.littlerect;
MyBigRect.SetMode(3,3);
OnNewgame();
}
这么用会有错误呢?
为什么没有重新分配程序还可以运行呢?
release下程序比较紧凑,所以如果访问了未分配的内存一般都是出错的。
不知道你有没有理解?我知道我的说法不是很专业.
int CBigRect::WGrid = 3;
int CBigRect::HGrid = 3;
并且初始化
littlerect = new CLittleRect[WGrid*HGrid];
也就是说littlerect的长度是9。setMode(4,4)后,
WGrid和HGrid都是4。在InitAllRect中
for(int i=0;i<WGrid;i++)
for(int j=0;j<HGrid;j++)
{
littlerect[i*WGrid+j].index = i*WGrid+j;
littlerect[i*WGrid+j].TYPE = 0;
}
当i=3,j=3时
i*Wgrid+j = 11,
littlerect[11]超过了你申请的内存。改为:
int CBigRect::WGrid = 4;
int CBigRect::HGrid = 4;
一切ok。
是你的内存越界引起的.