[ASAP]堆栈损坏 [英] [ASAP] Stack corruption

查看:52
本文介绍了[ASAP]堆栈损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我目前正在开发一个应用程序,其中包括着名的Boyer-Moore算法实现

。为此,我使用来自
Thierry Lecrq。 htmltarget =_ blank> http://www-igm.univ-mlv.fr/~lecroq/string/node14.html



但不幸的是,当我编译并运行它时,这个源代码(见下文)somtimes导致堆栈

损坏。


#define ASIZE 256 //二进制

#define MAX(a,b)((a)>(b)?(a):( b))

void preBmBc(char * x,int m,int bmBc []){

int i;


for(i = 0; i< ASIZE; ++ i)

bmBc [i] = m;

for(i = 0; i< m - 1; ++ i)

bmBc [x [i] ] = m - i - 1;

}

void后缀(char * x,int m,int * suff){

int f ,g,i;


suff [m - 1] = m;

g = m - 1;

for(i = m - 2; i> = 0; - i){

if(i> g&& suff [i + m - 1 - f]< i - g)

suff [i] = suff [i + m - 1 - f];

else {

if(i< g)

g = i;

f = i;

while(g> = 0&& x [g] == x [g + m - 1 - f])

--g;

suff [i] = f - g;

}

}

}


void preBmGs(char * x,int m,int * bmGs){

int i,j;

int * suff = new int [m];


后缀(x,m,suff);


for(i = 0; i< m; ++ i)

bmGs [i] = m;

j = 0;

for(i = m - 1; i> = -1; - i)

if(i == -1 || suff [i] == i + 1)

for(; j< m - 1 - i; ++ j)

if(bmGs [j] == m)

bmGs [j] = m - 1 - i;

for(i = 0; i< = m - 2; ++ i)

bmGs [m - 1 - suff [i]] = m - 1 - i;

删除后缀;

}

BOOL TUNEDBM(char * x, int m,char * y,int n,int& lastpos){


int i,j;

int * bmBc = new int(ASIZE) ;

int * bmGs = new int(m);

/ *预处理* /

preBmGs(x,m,bmGs);

pr eBmBc(x,m,bmBc);


/ *搜索* /

j = 0;

while(j< = n - m){

for(i = m - 1; i> = 0&& x [i] == y [i + j]; --i);

if(i< 0){

lastpos = j + m;

删除bmGs;

删除bmBc;

返回TRUE;

j + = bmGs [0];

}

否则

j + = MAX(bmGs [i],bmBc [y [i + j]] - m + 1 + i);

}

删除bmGs;

删除bmBc;

返回FALSE;

}


有没有人知道我做错了什么?我做了一些调试

,在我看来腐败发生在''preBmGs'' - 功能

- 但我可能错了所以更好不要依赖它。


非常感谢任何帮助或建议


Peter


PS:祝大家圣诞快乐!!!

解决方案

Peter Schmitz写道:

[...]

int * bmBc = new int(ASIZE);
int * bmGs = new int(m);


使用[],而不是()。


[...]删除bmGs;
删除bmBc;


[...]


使用删除[],而不是删除。


然而,这些错误会导致堆损坏,而不是堆栈

损坏。您看到了什么错误消息?


-

Tim Robinson(MVP,Windows SDK)
http://mobius.sourceforge.net/


感谢您的回答。根据你的建议,我改变了我的代码如下:


int * bmBc = new int [ASIZE];

int * bmGs = new int [m] ;

[...]

if(i< 0){

lastpos = j + m;

delete [] bmGs;

delete [] bmBc;

返回TRUE;

[...]

delete [] bmGs;

delete [] bmBc; //崩溃线

返回FALSE;


不幸的是,当我尝试删除bmBc时整个算法崩溃了。

调试在第1148行的dbgheap.c中显示错误(见下文)。

我收到的原始错误消息是运行时错误#2:Stack

bmG周围的腐败。


任何想法?


到目前为止非常感谢

Peter


dbgheap.c的摘录:


/ *如果我们还没有检查整个堆,至少检查一下

object * /

if(!(_ crtDbgFlag& _CRTDBG_CHECK_ALWAYS_DF))

{

/ * check no-mans-land gap * /

if(!CheckBytes(pHead-> gap,_bNoMansLandFill,nNoMansLandSize))

_RPT3(_CRT_ERROR," DAMAGE:%hs block(#%d)之前)在0x%p。\ n",

szBlockUseName [_BLOCK_TYPE(pHead-> nBlockUse)],

pHead-> lRequest,

(BYTE *)pbData(pHead));


if (!CheckBytes(pbData(pHead)+ pHead-> nDataSize,

_bNoMansLandFill,nNoMansLandSize))

_RPT3(_CRT_ERROR," DAMAGE:%hs block之后(# %d)在0x%p。\ n",

szBlockUseName [_BLOCK_TYPE(pHead-> nBlockUse)],

pHead-> lRequest,
(BYTE *)pbData(pHead)); //撞车线

}


//////////////////////// ////////////////////////////////////////////////// //////////////


Tim Robinson写道:

Peter Schmitz写道:
[blockquote class =post_quotes> int * bmBc = new int(ASIZE);
int * bmGs = new int(m);



使用[],而不是()。

[...]

删除bmGs;
删除bmBc;


[...]

使用delete [],而不是删除。

然而,这些错误会导致堆损坏,而不是堆栈损坏。您看到了什么错误消息?

- Tim Robinson(MVP,Windows SDK)
http://mobius.sourceforge.net/



Peter Schmitz写道:

[...]

不幸的是,当我尝试删除bmBc时,整个算法现在崩溃。
调试在第1148行显示dbgheap.c中的错误(请参阅我收到的原始错误信息是''运行时错误#2'':堆栈
腐败的bmGs。



[.. 。]


如果您还没有这样做,请从Microsoft网站下载''调试工具,用于
Windows''。然后运行gflags.exe,输入可执行文件的

名称,并启用页面堆。


执行此操作会导致程序在尝试时崩溃

访问未分配的内存,而不是稍后崩溃。

一旦完成,请记得禁用页面堆。


-

Tim Robinson(MVP,Windows SDK)
http://mobius.sourceforge.net/


Hi,

I''m currently developing an application that includes an implementation of
the well-known Boyer- Moore algorithm. For this, I use the implementation of
Thierry Lecrq from http://www-igm.univ-mlv.fr/~lecroq/string/node14.html.

But unfortunately, this source code (see below) somtimes causes a stack
corruption when I compile and run it.

#define ASIZE 256 //binary
#define MAX(a, b) ((a) > (b) ? (a) : (b))

void preBmBc(char *x, int m, int bmBc[]) {
int i;

for (i = 0; i < ASIZE; ++i)
bmBc[i] = m;
for (i = 0; i < m - 1; ++i)
bmBc[x[i]] = m - i - 1;
}
void suffixes(char *x, int m, int *suff) {
int f, g, i;

suff[m - 1] = m;
g = m - 1;
for (i = m - 2; i >= 0; --i) {
if (i > g && suff[i + m - 1 - f] < i - g)
suff[i] = suff[i + m - 1 - f];
else {
if (i < g)
g = i;
f = i;
while (g >= 0 && x[g] == x[g + m - 1 - f])
--g;
suff[i] = f - g;
}
}
}

void preBmGs(char *x, int m, int *bmGs) {
int i, j;
int *suff = new int[m];

suffixes(x, m, suff);

for (i = 0; i < m; ++i)
bmGs[i] = m;
j = 0;
for (i = m - 1; i >= -1; --i)
if (i == -1 || suff[i] == i + 1)
for (; j < m - 1 - i; ++j)
if (bmGs[j] == m)
bmGs[j] = m - 1 - i;
for (i = 0; i <= m - 2; ++i)
bmGs[m - 1 - suff[i]] = m - 1 - i;
delete suff;
}
BOOL TUNEDBM(char *x, int m, char *y, int n, int &lastpos) {

int i, j;
int *bmBc = new int(ASIZE);
int *bmGs = new int(m);
/* Preprocessing */
preBmGs(x, m, bmGs);
preBmBc(x, m, bmBc);

/* Searching */
j = 0;
while (j <= n - m) {
for (i = m - 1; i >= 0 && x[i] == y[i + j]; --i);
if (i < 0) {
lastpos = j + m;
delete bmGs;
delete bmBc;
return TRUE;
j += bmGs[0];
}
else
j += MAX(bmGs[i], bmBc[y[i + j]] - m + 1 + i);
}
delete bmGs;
delete bmBc;
return FALSE;
}

Does anyone have an idea on what I''m doing wrong? I''ve done some debugging
and it seems to me that the corruption takes place at the ''preBmGs''- function
- but I might be wrong so better don''t rely on it.

Thanks a lot for any help or suggestion

Peter

P.S.: Merry Christmas to everyone!!!

解决方案

Peter Schmitz wrote:
[...]

int *bmBc = new int(ASIZE);
int *bmGs = new int(m);
Use [], not ().

[...] delete bmGs;
delete bmBc;


[...]

Use delete[], not delete.

However, these mistakes would cause heap corruption, not stack
corruption. What error message are you seeing?

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/


Thanks for answering. At your advice, I changed my code as follows:

int *bmBc = new int[ASIZE];
int *bmGs = new int[m];
[...]
if (i < 0) {
lastpos = j + m;
delete[] bmGs;
delete[] bmBc;
return TRUE;
[...]
delete[] bmGs;
delete[] bmBc; //Line of Crash
return FALSE;

Unfortunately, the whole algorithm now crashes when I try to delete bmBc.
The debugging shows an error in dbgheap.c at line 1148 (see below).
The original error message I received was a ''Run-Time error #2'': Stack
corruption around bmGs.

Any ideas?

Thanks a lot so far
Peter

Extract of dbgheap.c:

/* if we didn''t already check entire heap, at least check this
object */
if (!(_crtDbgFlag & _CRTDBG_CHECK_ALWAYS_DF))
{
/* check no-mans-land gaps */
if (!CheckBytes(pHead->gap, _bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: before %hs block (#%d) at 0x%p.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));

if (!CheckBytes(pbData(pHead) + pHead->nDataSize,
_bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: after %hs block (#%d) at 0x%p.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead)); //line of crash
}

////////////////////////////////////////////////////////////////////////////////////////

"Tim Robinson" wrote:

Peter Schmitz wrote:
[...]

int *bmBc = new int(ASIZE);
int *bmGs = new int(m);



Use [], not ().

[...]

delete bmGs;
delete bmBc;


[...]

Use delete[], not delete.

However, these mistakes would cause heap corruption, not stack
corruption. What error message are you seeing?

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/



Peter Schmitz wrote:
[...]

Unfortunately, the whole algorithm now crashes when I try to delete bmBc.
The debugging shows an error in dbgheap.c at line 1148 (see below).
The original error message I received was a ''Run-Time error #2'': Stack
corruption around bmGs.


[...]

If you haven''t already done so, download the ''Debugging Tools for
Windows'' from the Microsoft web site. Then run gflags.exe, enter the
name of your executable, and enable Page Heap.

Doing this will cause your program to crash the moment it tries to
access unallocated memory, instead of crashing at some point later on.
Remember to disable Page Heap once you are done.

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/


这篇关于[ASAP]堆栈损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆