Cist(BYTE *a, int width, int height)
{
int wth,hgt; //扩展图像的宽和高
wth = width + 2;
hgt = height + 2;
long len=wth*hgt; BYTE *b = new BYTE[len]; //!该行编译通过,执行时有时后报错 for( int i = 0; i < hgt; i++) //拓展的边缘补0
for ( int j =0; j < wth; j++ )
{
if ( j%wth == 0 || (j+1)%wth == 0 ||
i == 0 || i == hgt-1 )
b[i*wth + j] = 0;
else
b[i*wth + j] = a[(i-1)*width+(j-1)];
}
}在函数内开辟空间报错,len=70000左右. 为什么会显示 0xc00000005 access Violation 错误!
这不是在堆上开辟的空间吗? 我在其他类的函数里面开辟更多的空间都没有问题.
{
int wth,hgt; //扩展图像的宽和高
wth = width + 2;
hgt = height + 2;
long len=wth*hgt; BYTE *b = new BYTE[len]; //!该行编译通过,执行时有时后报错 for( int i = 0; i < hgt; i++) //拓展的边缘补0
for ( int j =0; j < wth; j++ )
{
if ( j%wth == 0 || (j+1)%wth == 0 ||
i == 0 || i == hgt-1 )
b[i*wth + j] = 0;
else
b[i*wth + j] = a[(i-1)*width+(j-1)];
}
}在函数内开辟空间报错,len=70000左右. 为什么会显示 0xc00000005 access Violation 错误!
这不是在堆上开辟的空间吗? 我在其他类的函数里面开辟更多的空间都没有问题.
if ( pWinThread != NULL )
{
lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);
TRACE1("Warning: Uncaught exception in WindowProc (returning %ld).\n",
lResult);
}按F11单步运行,进入了这里,在
lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);
出现错误.new 一块内存这么麻烦,急切请高手指点
另外不止b会越界,a也会。
b[i*wth + j] = a[(i-1)*width+(j-1)]; 你要判断一下是哪个越界了。
下面的复制还没有执行.delete []b 代码加在函数结束前 .
或者在调试选项中Exceptions开启 Cxxxx5一项, 到时就停下来了,估计还是访问越界
b[i*wth + j] = a[(i-1)*width+(j-1)]; 中的 a[(i-1)*width+(j-1)] 数组下标会成为很大的负值
访问越界
BYTE *b = new BYTE[len]; //!该行编译通过,执行时有时后报错
很有可能是在此之前已经有内存错误发生的,通常是越界或者释放后还被访问过。但是没有立马表现出来。直到又有new的动作时才发现。