//this is extlcs.c
#include <stdio.h>
#include <stdlib.h>
#include "extlcs.h"
typedef struct {
long long *row;
long nRow;
long long *col;
long nCol;
}ttable;
int Max(int a, int b){
if(a>b) return a;
else return b;
}
int calcLcs(const long long *row, long Nrow, long Ncol, const long long *col){
int matrix[Nrow+1][Ncol+1];
int i,j;
for(i=0;i<Nrow;i++){
matrix[i][0]= 0 ;
}
for(j=0;j<Ncol;j++){
matrix[0][j] = 0;
}
for(i=1;i<Nrow;i++){
for(j=1;j<Ncol;j++){
if(row[i-1]==col[j-1]) matrix[i][j] = matrix[i-1][j-1] + 1;
else matrix[i][j] = Max(matrix[i][j-1],matrix[i-1][j]);
}
}
printf("%d", matrix[i][j]);
return matrix[i][j];
}
int extlcs(char *input1, char *input2, char *output) {
FILE *prova1 = fopen(input1, "r");
FILE *prova2 = fopen(input2, "r");
ttable *table = (ttable*) malloc(sizeof(ttable));
fseek(prova1,0L,SEEK_END);
table->nRow = ftell(prova1);
fseek(prova1, 0L, SEEK_SET);
printf("%ld\n", table->nRow);
table->row = 0;
table->row = (long long*) malloc(table->nRow);
fseek(prova2,0L,SEEK_END);
table->nCol = ftell(prova2);
fseek(prova2, 0L, SEEK_SET);
printf("%ld\n", table->nCol);
table->col = 0;
table->col = (long long*) malloc(table->nCol);
fread(table->row, sizeof(long long), table->nRow, prova1);
fread(table->col, sizeof(long long), table->nCol, prova2);
int res = calcLcs(table->row,table->nRow,table->nCol,table->col);
printf("%d",res);
/*
int vet[40];
n=fread(vet,sizeof(int),40,prova1);
for(i=0;i<40;i++) printf("%d ", vet[i]);
printf("%d",n);
fclose(prova2);
*/
FILE *out = fopen(output, "w");
printf((const char *) out, "%d\n", res);
fclose(prova1);
fclose(prova2);
fclose(out);
return res;
}
//this is main.c
#include <stdio.h>
#include "extlcs.h"
int main(int argc, char** argv) {
if(argc!=4){
printf("errore");
return -1;
}
extlcs(argv[1],argv[2],argv[3]);
return 0;
}
Asked
Active
Viewed 29 times
-1

user438383
- 5,716
- 8
- 28
- 43

Andrea
- 1
-
Please narrow down where/when you get the segfault. – Yunnosch Mar 09 '22 at 10:05
-
Explain `malloc(table->nRow)`. It looks too small, only space for nrow bytes, but needs space for nrow long long as far as I can tell. – Yunnosch Mar 09 '22 at 10:07
-
i tryied to add "sizeof long long" and multiple it per nrow but i still get segmentation fault – Andrea Mar 09 '22 at 10:24
-
i also tryied to put some "printf" to understand where the program stops but it always says segmentation fault without any "printf" – Andrea Mar 09 '22 at 10:29
-
You get a segfault before any output, even if the printf has a newline/flush and is the first line in `main()`? Weird. – Yunnosch Mar 09 '22 at 10:36
-
yes i get it also if i put the printf in the very first line – Andrea Mar 09 '22 at 10:39
-
Can you explain the lines which I point out as suspicious in my answer? – Yunnosch Mar 09 '22 at 10:43
-
we need to allocate in memory "space" * "row array" that has nrow lenght – Andrea Mar 09 '22 at 10:47
-
Exactly. Now, where in your code is the `space *`part? I only see the `nRow` part. What you have is `1 * nRow`. You state yourself that you need `size * nRow` and for `long long`size surely is > 1. – Yunnosch Mar 09 '22 at 10:56
-
i tryied to put sizeof long long as space, but it stil doesn't work :( – Andrea Mar 09 '22 at 10:58
-
Did you twice? Or better "everywhere". – Yunnosch Mar 09 '22 at 10:59
-
I am sure that the size is part of the problem here. But you probably want tto create a new question focused on how you get a segfault before the first lilne in `main()`. Make sure to have [mre] for that in the new question. – Yunnosch Mar 09 '22 at 11:02
-
i did it in table col and table row, should i do it in other part? – Andrea Mar 09 '22 at 11:04
-
You should review all your allocations. Size, dimension, do not repeat type, do not cast. For debugging make a [mre], creating one is debuggin and very helpful for asking questions. – Yunnosch Mar 09 '22 at 11:06
1 Answers
0
I suspect malloc(table->nRow)
.
It looks too small, only space for nRow bytes, but needs space for nRow long long
as far as I can tell.
Similar for malloc(table->nCol)
.
You probably want to multiply by sizeof(long long)
.
And better look up some best practices on malloc. (E.g. not repeating types and not casting.)

Yunnosch
- 26,130
- 9
- 42
- 54