473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[ASAP] Stack corruption

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!!!
Nov 17 '05 #1
14 1700
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/
Nov 17 '05 #2
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/

Nov 17 '05 #3
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/
Nov 17 '05 #4
I downloaded the 6.4 version of the debugging tools, ran gflags.exe, chose
'Verifier', entered the name of my process, enabled pageheap and clicked on
OK - unfortunately this does not change anything. The application still
crashes at this certain point.

Any ideas?

Thanks
Peter

"Tim Robinson" wrote:
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/

Nov 17 '05 #5
I'm afraid page heap only works for HeapAlloc functions, not CRT allocation
(especially the debug one, where it reserves some guard space).
It's better just to set in the debugger a data breakpoint at the allocation
end, immediately after allocating the array. This way, the VC debugger will
stop the program.

"Tim Robinson" <ti**********************@nowhere.com> wrote in message
news:32*************@individual.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/

Nov 17 '05 #6
Alexander Grigoriev wrote:
I'm afraid page heap only works for HeapAlloc functions, not CRT allocation
(especially the debug one, where it reserves some guard space).
It's better just to set in the debugger a data breakpoint at the allocation
end, immediately after allocating the array. This way, the VC debugger will
stop the program.


Pageheap works fine for CRT allocations, because unless you have VC++ 5
compatibility turned on, malloc and new go straight to HeapAlloc.

Though you're correct in saying that it works best on Release builds.

--
Tim Robinson (MVP, Windows SDK)
http://mobius.sourceforge.net/
Nov 17 '05 #7
Hi again! I've done some deeper debugging and therefore some information to
share:
It seems (!) that the stack corruption only occurs when length of the buffer
to search in is bigger by one (or equal) to the length of the search pattern
(6:5 or 6:6 in my debugging). Can't see an error in the algorithm that
supports this theory, can you?

Thanks so far
Peter

"Peter Schmitz" wrote:
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!!!

Nov 17 '05 #8
"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
Any ideas?


How about posting a complete, standalone program with parameters that cause
it to crash. I have some ideas, but I'd like to see the whole thing first.

Please include a main() function with suggested parameters.

Steve
--
Steve Friedl -- Tustin, California USA -- www.unixwiz.net
Unix Wizard -- Microsoft MVP/Security -- I speak for me only
Nov 17 '05 #9
And, on top of this advice, DO NOT use the debug version of the C-Rutnime.
It adds trailing patterns to the allocations that can delay
the detection of the problem with PageHeap.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Tim Robinson" <ti**********************@nowhere.com> wrote in message
news:32*************@individual.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/

Nov 17 '05 #10

"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
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/


Is it possible that you delete bmBc twice? I would suggest that you set bmBC
to NULL immeditely after delete [] bmBc.
In addition, check if bmBC <> NULL before deleting it. What's the value of
ASIZE? I guess there could be a problem if it's zero.

/ Fredrik

Nov 17 '05 #11
Two approaches:

1. Use data access breakpoint.
2. Put ASSERT before each access, to validate the index against the array
size.

"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
Hi again! I've done some deeper debugging and therefore some information
to
share:
It seems (!) that the stack corruption only occurs when length of the
buffer
to search in is bigger by one (or equal) to the length of the search
pattern
(6:5 or 6:6 in my debugging). Can't see an error in the algorithm that
supports this theory, can you?

Thanks so far
Peter

"Peter Schmitz" wrote:
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!!!

Nov 17 '05 #12
You write (which allocates 1 int and initializes with ASIZE/m):
int *bmBc = new int(ASIZE);
int *bmGs = new int(m);

instead of (allocates ASIZE/m ints, unitialized):
int *bmBc = new int[ASIZE];
int *bmGs = new int[m];

- Mete Ciragan

"Peter Schmitz" <Pe**********@discussions.microsoft.com> schrieb im
Newsbeitrag news:ED**********************************@microsof t.com...
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!!!

Nov 17 '05 #13
You also have mismatched new[]/delete:
int *suff = new int[m];
...
delete suff;
This should be delete[].

"Mete Ciragan" wrote:
You write (which allocates 1 int and initializes with ASIZE/m):
int *bmBc = new int(ASIZE);
int *bmGs = new int(m);

instead of (allocates ASIZE/m ints, unitialized):
int *bmBc = new int[ASIZE];
int *bmGs = new int[m];
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;
}

Nov 17 '05 #14
Looks like code from the school of "the shorter my variable names the faster
my code will run".

Rewrite it! It's horrible write-only code. Use vectors and strings instead
of arrays and char pointers.

Stu
"Peter Schmitz" <Pe**********@discussions.microsoft.com> wrote in message
news:ED**********************************@microsof t.com...
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!!!

Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ilia Poliakov | last post by:
I have a static function in the class like this: class A { static void f1() } void A::f1() { .....
3
by: harry | last post by:
Hi all I am putting a code snippet #include "stdio.h" #include "conio.h" struct base{ int i; char c; };
18
by: Dev | last post by:
I'm working with some IBM sponsored C APIs to interface with a corporate legacy system, and it seems that I'm getting some stack corruption after any API call. I believe the APIs were designed...
1
by: Humber Consumer | last post by:
I have a simple question. I have following piece of code in a sample MFC application: void CSampleAppDlg::OnButton1() { try { char e; ...
6
by: Tony | last post by:
Is there any value to pursuing program designs that mimimize the mainline call stack? For example, within main() I could code up something like: while(GetMsg(m)) DispatchMsg(m); instead of...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
7
by: ifoundgoldbug | last post by:
Ok here is a short description of what i am doing. I need to program a dll for use by another program. the dll compiles fine but when i call it from another program i get a stack corruption of...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.