Segmentation Fault when redirecting input, but it works when it is by console

1

I have the following code:

#include <stdio.h>

#define WLEN      32    /* word length */
#define LOGWLEN   50016 /* log word length -- round(LOG2(WLEN) */
#define SMAX      50016 /* maximum string length -- multiple of WLEN */
#define BITMAX    128   /* maximum bit string length -- round(SMAX/WLEN) */
#define ALPHASIZE 26    /* alphabet size */

typedef unsigned int WORD;
typedef short unsigned int INDEX;   /* i: alpha[i] == char */

int nwords;             /* no. of words for bits */
WORD bit1[BITMAX];
WORD bit2[BITMAX];
WORD a_strings[ALPHASIZE][BITMAX];
WORD *pb1, *pb2, *t1;

WORD bitmask[LOGWLEN] =
        {0x55555555, 0x33333333, 0x0f0f0f0f, 0x00ff00ff, 0x0000ffff};

void bitops(last, cur, index)
WORD *last, *cur;
INDEX index;
{
    register WORD x, y;
    register int j;
    register WORD *a_s;
    register WORD top_borrow, bottombit;

    a_s = &a_strings[index][0];
    bottombit = 1;
    for (j = 0; j < nwords; j++) {
        y = *(last++);
        x =  y | *(a_s++);
        top_borrow = (y >> (WLEN - 1)) & 0x1;
        y = ((y << 1) | bottombit);
        if (x < y)
            top_borrow = 1;
        *(cur++) = x & ((x - y) ^ x);
        bottombit = top_borrow;
    }
    return;
}

void alphastrings(s, len)
INDEX *s;
int len;
{
    register INDEX *p;
    register int i, j;

    for (i = 0; i < ALPHASIZE; i++)
        for (j = 0; j < nwords; j++)
            a_strings[i][j] = 0;
    p =  s;
    j = len;
    for (i = 0; i < j; i++)
        a_strings[*(p++)][i/WLEN] |=  1 << (i % WLEN);

    return;
}

int bitcount(wp)
WORD *wp;
{
    register WORD w, count;
    register int j, rshift, i;

    count = 0;
    for (i = 0; i < nwords; i++) {
        w = *(wp++);
        rshift = 1;
        for (j = 0; j < LOGWLEN; j++) {
            w = (w & bitmask[j]) + ((w & ~bitmask[j]) >> rshift);
            rshift <<= 1;
        }
        count += w;
    }
    return (count);
}


int bitlcs(a, lena, b, lenb)
INDEX *a;
int lena;
INDEX *b;
int lenb;
{
    register int i;
    register INDEX *pbstring;
    register int j, k;

    nwords = (lena + WLEN - 1) / WLEN;

    alphastrings(a, lena);

    pb1 = &bit1[0];
    for (i = 0; i < nwords; i++)
        *pb1++ = 0;     /*** bit1[i] = 0; ***/
    pb1 = &bit1[0]; pb2 = &bit2[0];
    pbstring = b;

    for (i = 1; i <= lenb; i++) {
        bitops(pb1, pb2, *(pbstring++));
        t1 = pb1; pb1 = pb2; pb2 = t1;
    }
    return (bitcount(pb1));
}

int leer(INDEX * s) {
    int c, index, i = 0;
    while((c = getchar()) != EOF && c != '\n') {
        s[i++] = (int) c - 97; 
    }
    return i;
}

int main(int argc, char const *argv[])
{   
    INDEX stringa[SMAX];// = {1, 1, 1};
    INDEX stringb[SMAX];// = {1, 2, 1};
    int lenA, lenB;
    lenA = leer(stringa);
    lenB = leer(stringb);
    printf("%d\n", bitlcs(stringa, lenA, stringb, lenB));
    return 0;
}

I'm testing it with 50000 letters "a" and 50000 letters "e" as input, for that I put together a file with those letters. The problem is that when I compile with:

gcc -O2 miPrograma.c -o miPrograma.exe

And I execute it with ./miPrograma.exe < strings.txt

Throw Segmentation Fault , although if I execute it simply ./miPrograma.exe and copy and paste both strings works correctly.

I also proved that debugging it with

gcc -O2 miPrograma.exe -g
gdb ./a.out
run < strings.txt

It also works perfectly, so I can not see where the problem is. I do not know what to do, if someone could give me a hand I would appreciate it.

UPDATE

I have noticed that when the number of characters decreases there is no problem with redirections, so it is clearly a problem when storing the 50000 of each letter.

    
asked by Genarito 12.01.2018 в 18:19
source

1 answer

1

There I solved the problem.

As for the SIGSEGV was in the presence of a Heisenbug as explained in this question . Disabling the memory randomization in the debugger was solved:

(gdb) set disable-randomization off

As for the algorithm, I had a problem of setting the macro BITMAX (whose value is established from the rounding of the SMAX / WLEN division) which had been left with the value 128 and I had forgotten to change it to the corresponding (1563) when updating the SMAX and WLEN values.

    
answered by 13.01.2018 / 17:43
source