【问题标题】:C : double pointer passed as parameters comes back empty [duplicate]C:作为参数传递的双指针返回空[重复]
【发布时间】:2016-02-03 13:21:32
【问题描述】:

起初,如果我在函数内释放分配可能听起来很正常,但事实并非如此。当我写这些行时,我找到了一个解决方法,但我想在我的代码中保持一定的同质性,并且更愿意保持它的方式,但你知道工作正常,所以还有其他解决方案或我的解决方法是唯一的选择吗?

主要功能:

void main(void)
{
    SHead head; // Custom struct
    unsigned char **array = NULL; // pointer to 2D array

    allocArray2D(&head, array) // the function signature: (SHead*, unsigned char**)

    // here, the array pointer is still NULL (0x0)
    //...

    return EXIT_SUCCESS;
}

分配函数 malloc 在 21 个 unsigned char* 附近分配了非常少量的内存,并且为每个简单指针分配了 21 个 unsigned char。 在函数内,指针很好,指向正确的地址。

所以我的解决方法是从以下位置修改函数:

void allocArray(SHead* h, unsigned char** arr)
{
    int x, y, i;
    getsize(head, *x, *y);

    arr = (unsigned char**)malloc(sizeof(unsigned char*)*y);
    if(arr)
        printf(">> Erro allocating memory\n"), return;

    for(i =0; i<y; i++)
    {
        arr[i] = (unsigned char)malloc(sizeof(unsigned char)*x);
    }
}

到以下:

unsigned char** allocArray(SHead*)
{
    int x, y, i;
    unsigned char **arr;
    getsize(head, *x, *y);

    arr = (unsigned char**)malloc(sizeof(unsigned char*)*y);
    if(arr)
        printf(">> Erro allocating memory\n"), return;

    for(i =0; i<y; i++)
    {
        arr[i] = (unsigned char)malloc(sizeof(unsigned char)*x);
    }

    return arr; // returning the address
}

正如我之前所说,我希望在我的代码中保持同质性,并且希望保持与我拥有的其他函数相似的函数签名。我的解决方法工作正常。我想知道这是否是唯一的解决方案,或者我可能遗漏了一些东西。

编辑:在 cmets 之后,我添加了更多代码。

谢谢, 亚历克斯。

【问题讨论】:

  • 不应该是allocArray2D(&amp;head, array);还是错字?
  • 请出示allocArray2D函数。
  • 提供minimal reproducible example*head 看起来很不对劲。
  • allocArray2D 是什么,它应该做什么?您能尝试创建一个Minimal, Complete, and Verifiable Example 并展示给我们看吗?代码说一千多张图片。
  • 又一个添加星星而不是简单地返回结果的示例。通过使用更多的星星,您不会成为更好的 C 程序员。这不是做饭。

标签: c function pointers parameters


【解决方案1】:

您必须将指向二维数组的指针传递给您的函数,以便在函数中写入指针后面的值:

SHead head; // Custom struct
unsigned char **array = NULL; // pointer to 2D array

allocArray2D(*head, &array)
                 // ^ address of array  

-

void allocArray(SHead* head, unsigned char*** pArray)
                                  // ^ pointer to char** because its an output parameter
{
    int x, y, i;
    getsize( head, &x, &y );

    *pArray = malloc( y * sizeof( unsigned char * ); 
 // ^ assigne somtething to the variable array refered by the pointer pArray
    if( *pArray == NULL ) 
    {
        printf(">> Erro allocating memory\n")
        return;
    }

    for ( i = 0; i < y; i ++ )
        (*pArray)[i] = malloc( x * sizeof( unsigned char ) );
}

注意,您所做的是将NULL-pointe 传递给函数allocArray

另一种解决方案是通过函数allocArray的返回值返回分配的内存:

 SHead head; // Custom struct
 unsigned char **array = NULL;

 array = allocArray( &head );

-

 unsigned char** allocArray( SHead* head )
 {
     int x, y, i;
     getsize( head, &x, &y );

     unsigned char** arr = malloc( y * sizeof( unsigned char * );
     if( arr  == NULL ) 
     {
        printf(">> Erro allocating memory\n")
        return;
     }

     for ( int i = 0; i < y; i ++ )
         arr[i] = malloc( x * sizeof( unsigned  char ) );
     return arr;
 }

【讨论】:

  • 谢谢,现在我明白了。
【解决方案2】:

如果我对您的理解正确,您需要的是如下内容 为简单起见,我排除了引用结构的参数。

void allocArray2D( unsigned char ***a, size_t n )
{
    *a = malloc( n * sizeof( unsigned char * ) );

    for ( size_t i = 0; i < n; i++ ) ( *a )[i] = malloc( n * sizeof( unsigned char ) );
}

//...

unsigned char **array = NULL; // pointer to 2D array

allocArray2D( &array, 21 );

如果您希望在将原始对象传递给函数后对其进行更改,那么您必须通过引用传递它。

【讨论】:

    【解决方案3】:

    你的电话看起来很奇怪。

    首先,您传递的是 *head。 head 似乎是一个未初始化的变量,因此传递 *head 似乎非常错误。

    其次,被调用的函数无法看到数组。您的调用等效于 allocArray2D (*head, NULL) 并且根本没有数组变量。这似乎也很错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2013-03-16
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多