Home > Enterprise >  Why does memcpy() cause a crash?
Why does memcpy() cause a crash?

Time:08-14

I am trying to run an implementation of the Hashq algorithms (string matching algorithm). It was written for POSIX, but I am trying to run in Code Blocks on Windows.

The implementation is:

#define RANK5 5
#define WSIZE 256 // greater int value fitting in a computer word
#include <stdio.h>
#include <string.h>

int search(unsigned char *x, int m, unsigned char *y, int n) {
    int count, i, j, sh, shift[WSIZE], sh1, mMinus1, mMinus4;
    unsigned int h;
    if (m < 5)
        return -1; // substring or pattern has to be greater than 4

    /* Preprocessing */
    // BEGIN_PREPROCESSING
    count = 0;
    mMinus1 = m - 1;
    mMinus4 = m - 4;
    for (i = 0; i < WSIZE;   i)
        shift[i] = mMinus4;

    h = x[0];
    h = ((h << 1)   x[1]);
    h = ((h << 1)   x[2]);
    h = ((h << 1)   x[3]);
    h = ((h << 1)   x[4]);
    shift[h % WSIZE] = m - RANK5;
    for (i = RANK5; i < mMinus1;   i) {
        h = x[i - 4];
        h = ((h << 1)   x[i - 3]);
        h = ((h << 1)   x[i - 2]);
        h = ((h << 1)   x[i - 1]);
        h = ((h << 1)   x[i]);
        shift[h % WSIZE] = mMinus1 - i;
    }
    h = x[i - 4];
    h = ((h << 1)   x[i - 3]);
    h = ((h << 1)   x[i - 2]);
    h = ((h << 1)   x[i - 1]);
    h = ((h << 1)   x[i]);
    sh1 = shift[h % WSIZE];
    shift[h % WSIZE] = 0;
    if (sh1 == 0)
        sh1 = 1;
    // END_PREPROCESSING

    // BEGIN_SEARCHING
    i = mMinus1;
    memcpy(y   n, x, m);
    while (1) {
        sh = 1;
        while (sh != 0) {
            h = y[i - 4];
            h = ((h << 1)   y[i - 3]);
            h = ((h << 1)   y[i - 2]);
            h = ((h << 1)   y[i - 1]);
            h = ((h << 1)   y[i]);
            sh = shift[h % WSIZE];
            i  = sh;
        }
        if (i < n) {
            j = 0;
            while (j < m && x[j] == y[i - mMinus1   j])
                j  ;
            if (j >= m) {
                count  ; // OUTPUT(i-mMinus1);//printf("%d\n", i-mMinus1);count  ;//
            }
            i  = sh1;
        } else {
            // END_SEARCHING;
            return count;
        }
    }
}

int main() {
    char *x = "welcometot";
    char *y = "welcometotheStack Overflow,welcometoalllearners...welcome!";
    int m = strlen(x);
    int n = strlen(y);
    printf("\n The number of total matching: %d", search(x, m, y, n));
    return 0;
}

The problem is the code does not generate any output.

hash5.exe has stopped working

It shows:

Problem Event Name: APPCRASH

I found that if memcpy(y n, x, m); is removed, the code works and provide correct output! But why does this happen? Can anyone explain?

The algorithm is presented in the following research article:

S. Faro: A very fast string matching algorithm based on condensed alphabets, in Algorithmic Aspects in Information and Management - 10th International Conference, AAIM 2016. Proceedings, vol. 9778 of Lecture Notes in Computer Science, Springer, 2016, pp. 65–76

CodePudding user response:

Because char *y = "some text" will be placed in read only memory segment by the compile/link process. Try with char y[] = "some text" or char y[SOME_BIGGER_SIZE] = "some text"

  • Related