简体   繁体   中英

Segmentation fault with dynamic allocation

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?

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));
}

You forgot to make a point somewhere defined. In other words, you never initialize a .

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:

  • I've noticed that you didn't initialize a array
  • 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

  • I guessed that you wanted to write the matrix to the file as well so I've modified the code
  • I've added the free function at the end so that the valgrind won't complain
  • I've added some brackets here and there

Hope it will help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM