C : double pointer passed as parameters comes back empty

标签 c function pointers parameters

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

主要功能:

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
}

正如我之前所说,我希望在代码中保持同质性,并且更愿意保留与我拥有的其他函数类似的函数签名。我的解决方法工作正常。我想知道这是否是唯一的解决方案,或者也许我错过了一些东西。

编辑:根据评论,我添加了更多代码。

谢谢你, 亚历克斯。

最佳答案

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

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 指针传递给函数 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;
 }

关于C : double pointer passed as parameters comes back empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178254/

相关文章:

Cython:将 void* 转换为 double 和反向转换

c++ - Arduino UDP 错误 WiFiUdp.cpp

Matlab 求符号函数的零点

javascript - 评估代码的正确方法

c++ - 结构可以具有类型 `Card*` 吗? (书中的例子)

c - 如何检查 C 中的地址是否有效或已分配?

c - valgrind 错误和 ucontext。为什么是 "Use of uninitialised value of size 8"?

c - C中的Segmentation Fault 11 由较大的操作数引起

c - 对 C 代码中的多个位置使用相同的函数

c++ - 谷歌测试比较指针数组的内容