为什么内存没有分配给数组?
Why the memory is not allocated to the array?
我在大学时被要求用 C 构建一个函数,该函数从文件中读取多个值,然后动态地将内存分配给使用指针(它必须使用指针)通过引用传递的数组,然后我们被要求显示使用另一个函数读取的值。
我尝试了此处显示的 2 个方法:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
///Read an 1d array from the file stuff.txt inside of a function
/// and dinamically allocate memory
/// after that display on the screen the values readed from file
void getDataFirstFunction(int **a, int *n)
{
FILE *f;
f = fopen("stuff.txt" ,"r");
if(!f)
{
printf("ERROR WHILE OPENING THE FILE!!");
exit(0);
} else
{
int d;
fscanf(f,"%d",&(*n));
///we dinamically alocate memory
a = calloc((*n),sizeof(int));
int i=0;
while(!feof(f))
{
fscanf(f,"%d",&a[i++]);
}
}
///if I try to display the values here it works
///but when I try to read display them in the display
///function it is not going to work anymore
fclose(f);
}
int * getData(int **a,int *n)
{
FILE *f;
f = fopen("stuff.txt" ,"r");
if(!f)
{
printf("ERROR WHILE OPENING THE FILE!!");
exit(0);
} else
{
int d;
fscanf(f,"%d",&(*n));
///we dinamically alocate memory
a = calloc((*n),sizeof(int));
int i=0;
while(!feof(f))
{
fscanf(f,"%d",&a[i++]);
}
}
///if I try to display the values here it works
fclose(f);
return a;
}
void displayValues(int *a,int n)
{
for(int i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
int main()
{
int *a,*b,n,m;
getData(a,&n);
getDataFirstFunction(b,&m);
displayValues(a,n);
displayValues(b,m);
return 0;
}
然而,用 void 声明的第一个函数不起作用,我不明白为什么,因为我故意写了一个双指针参数,所以当我分配内存时,该内存不会丢失堆栈上的变量在函数完成执行后由编译器释放。
第二个名为 getData 的函数确实有意义并且可以工作,但我不知道我是否可以使第一个带有 void 的函数工作,因为似乎内存只分配给堆栈上的变量,然后在它完成执行时分配在没有将内存分配给 main 中的指针的情况下,对内存位置的引用消失了,所以当我尝试在屏幕上显示值时,程序冻结,然后我什么也得不到。
提前致谢。
在您的函数中,a
是指向指针的指针的副本。
您在这里为该副本分配了其他内容:
a = calloc((*n),sizeof(int));
在函数之外没有任何效果。
在你的函数之外,a
是一个指向 int 的指针,在那里写一个指向 int 的指针是有意义的。
你可以这样做(通过你的函数中使用的副本)例如像
*a = calloc((*n),sizeof(int));
对于函数之外的效果,您应该使用指向 int 的指针的地址进行调用
getData(&a,&n);
有了这个 int * getData(int **a,int *n)
,您可以将返回的指向 int 的指针分配给函数之外的 a
。但不要在这里getData(a,&n);
。对于返回指向 int 的指针,此行不合适 return a;
因为它 returns 指向指向 int 的指针。这确实恰好包含正确的值(新分配的指向 int 的指针),但它的类型仍然不正确。 ...
注:
您非常依赖 fscanf()
的工作,因此您用于大小的 n
可能未初始化...
你不应该使用 while(!feof(f))
,
Why is “while ( !feof (file) )” always wrong?
d
在你的函数中似乎没有被使用...
所以请注意M.M评论中的建议。关于阅读编译器警告。
我在大学时被要求用 C 构建一个函数,该函数从文件中读取多个值,然后动态地将内存分配给使用指针(它必须使用指针)通过引用传递的数组,然后我们被要求显示使用另一个函数读取的值。
我尝试了此处显示的 2 个方法:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
///Read an 1d array from the file stuff.txt inside of a function
/// and dinamically allocate memory
/// after that display on the screen the values readed from file
void getDataFirstFunction(int **a, int *n)
{
FILE *f;
f = fopen("stuff.txt" ,"r");
if(!f)
{
printf("ERROR WHILE OPENING THE FILE!!");
exit(0);
} else
{
int d;
fscanf(f,"%d",&(*n));
///we dinamically alocate memory
a = calloc((*n),sizeof(int));
int i=0;
while(!feof(f))
{
fscanf(f,"%d",&a[i++]);
}
}
///if I try to display the values here it works
///but when I try to read display them in the display
///function it is not going to work anymore
fclose(f);
}
int * getData(int **a,int *n)
{
FILE *f;
f = fopen("stuff.txt" ,"r");
if(!f)
{
printf("ERROR WHILE OPENING THE FILE!!");
exit(0);
} else
{
int d;
fscanf(f,"%d",&(*n));
///we dinamically alocate memory
a = calloc((*n),sizeof(int));
int i=0;
while(!feof(f))
{
fscanf(f,"%d",&a[i++]);
}
}
///if I try to display the values here it works
fclose(f);
return a;
}
void displayValues(int *a,int n)
{
for(int i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
int main()
{
int *a,*b,n,m;
getData(a,&n);
getDataFirstFunction(b,&m);
displayValues(a,n);
displayValues(b,m);
return 0;
}
然而,用 void 声明的第一个函数不起作用,我不明白为什么,因为我故意写了一个双指针参数,所以当我分配内存时,该内存不会丢失堆栈上的变量在函数完成执行后由编译器释放。
第二个名为 getData 的函数确实有意义并且可以工作,但我不知道我是否可以使第一个带有 void 的函数工作,因为似乎内存只分配给堆栈上的变量,然后在它完成执行时分配在没有将内存分配给 main 中的指针的情况下,对内存位置的引用消失了,所以当我尝试在屏幕上显示值时,程序冻结,然后我什么也得不到。
提前致谢。
在您的函数中,a
是指向指针的指针的副本。
您在这里为该副本分配了其他内容:
a = calloc((*n),sizeof(int));
在函数之外没有任何效果。
在你的函数之外,a
是一个指向 int 的指针,在那里写一个指向 int 的指针是有意义的。
你可以这样做(通过你的函数中使用的副本)例如像
*a = calloc((*n),sizeof(int));
对于函数之外的效果,您应该使用指向 int 的指针的地址进行调用
getData(&a,&n);
有了这个 int * getData(int **a,int *n)
,您可以将返回的指向 int 的指针分配给函数之外的 a
。但不要在这里getData(a,&n);
。对于返回指向 int 的指针,此行不合适 return a;
因为它 returns 指向指向 int 的指针。这确实恰好包含正确的值(新分配的指向 int 的指针),但它的类型仍然不正确。 ...
注:
您非常依赖 fscanf()
的工作,因此您用于大小的 n
可能未初始化...
你不应该使用 while(!feof(f))
,
Why is “while ( !feof (file) )” always wrong?
d
在你的函数中似乎没有被使用...
所以请注意M.M评论中的建议。关于阅读编译器警告。