[英]How to allocate memory using this malloc statement?
I have this method that reads a file. 我有这种读取文件的方法。 A matrix to be more specific where the first two numbers are the rows and the columns.
更具体的矩阵,其中前两个数字是行和列。 However when i try to allocate the memory using malloc and using the rows and columns the application crash.
但是,当我尝试使用malloc并使用行和列分配内存时,应用程序崩溃。
The code I'm using is this one: 我正在使用的代码是这样的:
#include stdio.h
#include stdlib.h
#include stdlib.h
float * readFile(char* nombre, int*renglones, int*columnas){
FILE *fp;
fp=fopen(nombre,"r");
fscanf(fp,"%d",&renglones);
printf("el numero de renglones es %d\n",renglones);
fscanf(fp,"%d",&columnas);
printf("number of rows %d\n",columnas);
float value;
fscanf(fp,"%f",&value);
printf("el numero de columnas es %f\n",value);
fscanf(fp,"%f",&value);
printf("el numero de columnas es %f\n",value);
printf("no llegue a malloc");
float * res = malloc(*renglones**columnas*sizeof(float)); //memory reservation and the line that breaks the program
printf("after malloc");
fclose(fp);
return 0;
}
I know that the includes are between <> 我知道包含在<>之间
The final code is this one with the includes: stdio.h and stdlib.h 最终的代码是这样的,其中包括:stdio.h和stdlib.h
float * readFile(char* nombre, int*renglones, int*columnas){
FILE *fp;
fp=fopen(nombre,"r");
fscanf(fp,"%d",renglones);
printf("el numero de renglones es %d\n",renglones);
fscanf(fp,"%d",columnas);
printf("el numero de columnas es %d\n",columnas);
float value;
float * res = (float*)malloc(*renglones**columnas*sizeof(float)); //Reserva de memoria
printf("llegue a malloc\n");
int i;
for(i=0;i<*renglones**columnas;i++){
fscanf(fp, "%f",&value);
res[i]=value;
printf("dato %f\n",value);
}
printf("%d",i);
fclose(fp);
return res;
}
Thanks! 谢谢!
Your problem is that you do: 您的问题是您这样做:
...
fscanf(fp,"%d",&renglones);
...
fscanf(fp,"%d",&columnas);
...
As a result, those 2 numbers (that you read from file) become pointers to renglones
and columnas
which you further dereference to calculate the size for malloc
. 结果,这两个数字(从文件中读取)成为指向
renglones
和columnas
指针,您可以进一步取消引用以计算malloc
的大小。 For instance, if you read numbers 16
and 32
, then renglones
points to 0x00000010
and columnas
points to 0x00000020
. 例如,如果您读取数字
16
和32
,则renglones
指向0x00000010
, columnas
指向0x00000020
。 However, these memory cells obviously contain random garbage (from the point of view of your task), ie they probably contain arbitrarily huge numbers which when multiplied together give even bigger number, and malloc
simply cannot allocate that much amount of memory, which ultimately results in crash. 但是,这些内存单元显然包含随机垃圾(从您的任务的角度来看),即它们可能包含任意大的数字,这些数字相乘会得到更大的数字,而
malloc
根本无法分配那么多的内存,最终导致在崩溃中。
Instead it should be: 相反,它应该是:
...
fscanf(fp,"%d",renglones);
...
fscanf(fp,"%d",columnas);
...
since both renglones
and columnas
are pointers already. 因为
renglones
和columnas
已经都是指针。 This way you would really fill the two int
variables to which renglones
and columnas
point to, and get the expected behavior. 这样,您就可以真正填充
renglones
和columnas
指向的两个int
变量,并获得预期的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.