Reputation: 125
Yes, I've read through the questions like this, but none of them proved useful in my problem. My program has a problem with a function that has been working perfectly in another one.
Program received signal SIGSEGV, Segmentation fault. _int_malloc (av=0x7ffff7dd8e40, bytes=32) at malloc.c:4703 4703 malloc.c: No such file or directory.
That's the error I recieve.
And here is my program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void endl()
{
printf("\n\n");
}
void loadVector(FILE *infile, double *vector, int col)
{
int i;
for(i=0;i<col;i++)
{
fscanf(infile,"%lf",&vector[i]);
}
printf("vector read...\n");
}
void printVec(double *vect, int run)
{
int k;
for(k=0;k<run;k++)
{
printf("%.2f ",vect[k]);
}
}
double* duplic(double* matr, double *vect, double *sol, int col, int row)
{
int k, l;
for(k=0;k<row;k++)
{
for(l=0;l<col;l++)
{
sol[k] += matr[col*k+l] * vect[l];
}
}
return sol;
}
int main(int argc, char **argv)
{
endl();
printf("=== ===");
endl();
if(argc!=3)
{
perror("not enough arguments");
endl();
return 1;
}
char ins[300];
char *cut;
int row=0, col=0, rnr = 1, index = 0;
double temp;
double *mat = NULL;
double *vec = NULL;
double *res = NULL;
FILE *in;
in = fopen(argv[1], "r");
while(fgets(ins,sizeof(ins),in) != NULL)
{
col = 0;
cut = strtok(ins," ");
while(cut != NULL)
{
col++;
sscanf(cut,"%lf",&temp);
if(mat==NULL)
{
mat = malloc(sizeof(temp));
*mat = temp;
}
else
{
rnr ++;
mat = realloc(mat,sizeof(mat)*rnr);
index = rnr - 1;
*(mat+index)=temp;
}
cut = strtok(NULL," ");
}
row ++;
}
fclose(in);
printf("Matrix read...");
endl();
printf("Printing matrix: ");
endl();
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%.2f ",mat[col*i+j]);
}
printf("\n");
}
endl();
// printf("rows: %i\tcols: %i\n",row,col);
vec = malloc(sizeof(col));
FILE *inv;
inv = fopen(argv[2],"r");
loadVector(inv,vec,col);
endl();
printVec(vec,col);
endl();
res = malloc(sizeof(row));
res = duplic(mat,vec,res,col,row);
printf("Solution-vector calculated...");
endl();
printf("\nSolution vector:");
endl();
printVec(res,row);
endl();
free(mat);
free(vec);
free(res);
fclose(inv);
printf("Exitting\n");
return 0;
}
The program is a matrix - vector multiplication one, which reads an unknown sized matrix and a matching vector (also, if you can offer a better way to read a random-length line with a better code, I'd be happy). The problem comes at the multiplication code - though it should be working, as it is with another program I wrote where the size is fix. So it reads the matrix and vector nicely, then gives the segfault.
Any thoughts?
Upvotes: 0
Views: 1614
Reputation: 2708
You write sizeof(col)
, but col
is an integer, which means your vec
array will always be allocated sizeof(int)
bytes of memory (which is typically 4 bytes but will depend on your architecture). If you want a double array with col number of doubles, you want to use malloc(sizeof(double) * col)
to allocate the right amount of bytes.
I suspect the same issue is occurring for your res
array. It might work in some cases where the space after the space you allocated is still available for write, in which case the computer doesn't complain, since malloc'ed memory is all in the heap. However, if it so happens that you try to write to a protected or reserved section of memory after the malloc'ed block (which is too small), the OS will throw a SegFault.
Upvotes: 2