[英]Segmentation fault with dynamic allocation
I wrote some code using malloc
function and make outcome file, but it shows segmentation fault. 我使用malloc
函数编写了一些代码,并生成了结果文件,但它显示了分段错误。 Could you give some advice? 你能给点建议吗?
It get m
and n
value, and make m
by n
matrix with a_ij=i*i+j*j
. 它获得m
和n
值,并通过a_ij=i*i+j*j
的n
矩阵构成m
。
#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. 你忘了分配的指针数组a
在你的程序的开头权。
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. 你忘了做a
地方定义的点。 In other words, you never initialize a
. 换句话说,你永远不会初始化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: 变化:
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) == 8958 ==无效读取大小4 == 8958 == at 0x109024:main(test.c:39)== 8958 ==地址0x51e4098是在分配了大小8的块之后的0字节== 8958 ==在0x4C2C840处:malloc(在/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)== 8958 ==由0x108E30:main(test.c:21)
related to the improper iteration over the a array 与数组的不正确迭代有关
Hope it will help :) 希望它会有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.