Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在函数中分配动态数组,并用C语言将其发送回调用方_C_Memory Management - Fatal编程技术网

在函数中分配动态数组,并用C语言将其发送回调用方

在函数中分配动态数组,并用C语言将其发送回调用方,c,memory-management,C,Memory Management,我已经使用java很长时间了,但出于某种原因,我需要使用C(ANSIC而不是C++)来编写简单的代码。我需要将指针从外部传递到函数,为指针分配一些内存,并在函数返回之前分配一些值。我的代码如下 #include <stdio.h> #include <stdlib.h> void test(int *a) { int n=3; // I have to call another function t determine the size of the array

我已经使用java很长时间了,但出于某种原因,我需要使用C(ANSIC而不是C++)来编写简单的代码。我需要将指针从外部传递到函数,为指针分配一些内存,并在函数返回之前分配一些值。我的代码如下

#include <stdio.h>
#include <stdlib.h>

void test(int *a)
{
  int n=3;
  // I have to call another function t determine the size of the array
  n = estimatesize(); // n >=3
  // I tried fix size n=10 also

  a = (int*)malloc(n*sizeof(int));
  a[0] = 1;
  a[1] = 2;
  a[2] = 3;
}

void main(void)
{
  int *s=NULL;
  test(s);
  printf("%d %d %d", s[0], s[1], s[2]);
}
#包括
#包括
无效测试(int*a)
{
int n=3;
//我必须调用另一个函数来确定数组的大小
n=estimatesize();//n>=3
//我也尝试过固定大小n=10
a=(int*)malloc(n*sizeof(int));
a[0]=1;
a[1]=2;
a[2]=3;
}
真空总管(真空)
{
int*s=NULL;
测试;
printf(“%d%d%d”,s[0],s[1],s[2]);
}

我不知道为什么代码会崩溃。我一开始以为是estimatesize()返回了错误的数字,但即使我将n改为10,错误仍然存在。所以我不能传递一个指向内存分配函数的指针?如果是这样,如何在函数内部动态创建内存并将其传递出去?我知道这可能是一个安全的问题,但我只想知道它是否可能,以及如何做到这一点。谢谢。

有两种解决方案:从函数返回指针,或通过引用传递参数

对于第一个,您不接受任何参数,而是返回指针:

int *test(void)
{
    int *a = malloc(...);
    ...
    return a;
}

int main(void)
{
    int *s = test();
    ...
}
对于第二个,您需要使用操作符
的地址传递指针的地址,换句话说,是指向指针的指针:

void test(int **a)
{
    *a = malloc(sizeof(int) * 3);

    for (int i = 0; i < 3; ++i)
        (*a)[i] = i;
}

int main(void)
{
    int *s;
    test(&s);
    ...
}
无效测试(int**a)
{
*a=malloc(sizeof(int)*3);
对于(int i=0;i<3;++i)
(*a)[i]=i;
}
内部主(空)
{
int*s;
测试(s&s);
...
}


它现在不工作的原因是,指针(
main
中的
s
)是通过复制来传递的。因此函数
test
有一个本地副本,其范围仅在
test
函数中。
test
函数中对
a
的任何更改都将在函数返回后丢失。由于参数中的
s
被复制,这意味着
main
中的
s
实际上从未更改值,函数调用后它仍然是
NULL

感谢您的回复。我明白你的想法,但我试过你的代码,它不起作用。同样,你在C++中实现它,但是我需要ANSI C,只有ANSI C编译器。@ USE12854,不,两个解决方案都是纯C,里面没有C++。@ USE12854 19数组不符合,记住这个口头禅:C是一个按值的参数语言。如果要按地址传递参数,那么传递地址正是您需要做的:传递目标变量的地址,并将参数声明为指向类型的正式指针。“类型”也没什么区别。无论目标变量类型是什么,指向该类型的指针就是参数的声明方式(在上面的Joachim代码中,类型是
int*
)。(1),我认为循环中定义的语法只在C++中工作。在ANSI C中,我们需要在代码之前定义所有变量。不管怎么说,通过修好它,我现在已经开始运行了。非常感谢:)@user1285419在
for
循环中声明变量,这样的循环已经标准化了很长时间(自C99标准以来)。您使用的是什么编译器及其版本?