Aashish Kumar Aashish Kumar -4 years ago 267
C Question

How to resolve the error :- *** stack smashing detected ***: ./sparse terminated Aborted (core dumped)

I'm unable to resolve this error,please help me where i'm wrong, also please tell me how to use the GNU debugger because when i'm using it, "1.c is not an executable file" error occurs.

Here's the ADT file of my code :- Sparse.c

Here's my code:-

#include <stdio.h>
#include <math.h>
#include "sparse.c"
int main(int argc, char* argv[]){
sparseMat *x,*y;
x=newSparse(100,1,0);
char filename[4];
int i,j,vectorCount=0;
float pow;
FILE *refFile;
refFile=fopen("x3.txt","r");
for(i=0; i<=100; i++){
fscanf(refFile,"%f",&x[i].value);
x[i].row=i;
x[i].col=1;
}
for(i=1; i<=10; i++){
sprintf(filename,"data/y%d.txt",i);
FILE *fptr=fopen(filename,"r");
y=newSparse(100,1,0);
for(j=0; j<100; j++){
fscanf(fptr,"%f",&y[j].value);
y[j].row=j+1;
y[j].col=1;
}
if(classCheck(x, y)==1){
vectorCount++;
}
}
return 0;
}

Answer Source

You declare

char filename[4];

so filename can contain filenames up to 3 characters long (4-1 because of the NUL terminator).

and later you have this:

sprintf(filename,"data/y%d.txt",i);

and "data/y%d.txt" is obviously longer than 3 characters.

So declaring char filename[100]; should do the job.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download