Reputation:
I wrote some code using malloc
function and make outcome file, but it shows segmentation fault. Could you give some advice?
It get m
and n
value, and make m
by n
matrix with a_ij=i*i+j*j
.
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
int i,j,m,n;
float **a, sum;
float func(float **a,int m,int n);
FILE *out;
int main()
{
printf("Enter the value of m and n: \n");
scanf("%d",&m);
scanf("%d",&n);
for(i=0; i<m; i++) a[i]=(float *)malloc(n*sizeof(float));
printf("o\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
a[i][j]=0;
func(a,m,n);
printf("\n matrix A: \n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
printf("%f\t", a[i][j]);
printf("\n");
sum=sum+a[i][j];
}
printf("\n SUM: %f",sum);
out=fopen("outFile","w");
fprintf(out,"\n matrix A: \n");
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
printf("%f\t", a[i][j]);
printf("\n");
}
fprintf(out,"\n SUM: %f", sum);
fclose(out);
return 0;
}
float func(float **a,int m,int n)
{
for(i=0; i<m; i++)
for(j=0; j<n; j++)
a[i][j]=(i+1)*(i+1)+(j+1)*(j+1);
return;
}
And how to correct this program to work?
Upvotes: 1
Views: 97
Reputation: 161
That's the program after my corrections:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
int i, j, m, n;
float **a, sum;
void func( float **a, int m, int n );
FILE *out;
int main()
{
printf( "Enter the value of m and n: \n" );
scanf("%d",&m);
scanf("%d",&n);
a = ( float** ) malloc( m * sizeof( float* ) );
for( i = 0; i < m; i++ )
{
a[ i ] = ( float * ) malloc( n * sizeof( float ) );
}
printf( "o\n" );
for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
{
a[ i ][ j ] = 0;
}
}
func(a,m,n);
printf("\n matrix A: \n");
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < n; j++ )
{
printf( "%f\t", a[ i ][ j ] );
sum = sum + a[ i ][ j ];
}
printf( "\n" );
}
printf( "\n SUM: %f", sum );
out = fopen( "outFile","w" );
fprintf( out, "\n matrix A: \n" );
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < n; j++ )
{
fprintf( out, "%f\t", a[ i ][ j ] );
}
fprintf( out, "\n");
}
fprintf( out,"\n SUM: %f", sum );
fclose( out );
for( i = 0; i < m; ++i )
{
free( a[ i ] );
}
free( a );
return 0;
}
void func( float **a, int m, int n )
{
for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
{
a[ i ][ j ] = ( i + 1 ) * ( i + 1 ) + ( j + 1 ) * ( j + 1 );
}
}
}
changes:
There was another problem:
==8958== Invalid read of size 4 ==8958== at 0x109024: main (test.c:39) ==8958== Address 0x51e4098 is 0 bytes after a block of size 8 alloc'd ==8958== at 0x4C2C840: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==8958== by 0x108E30: main (test.c:21)
related to the improper iteration over the a array
Hope it will help :)
Upvotes: 0
Reputation: 19050
You forgot to make a
point somewhere defined. In other words, you never initialize a
.
Upvotes: 1
Reputation: 22981
You forgot to allocate the array of pointers a
right at the beginning of your program.
First allocate the array which holds the pointers to the rows:
a = (float**)malloc(m*sizeof(float*));
then allocate memory for each row:
for(i=0; i<m; i++) {
a[i] = (float*)malloc(n*sizeof(float));
}
Upvotes: 1