繁体   English   中英

试图释放这个二维数组

[英]Trying to free this 2d array

当我运行该应用程序时,当它到达销毁功能时就会出现故障,我不知道为什么。 任何想法都可能来自分配函数,但是一切正常,直到我创建了destroy函数。

int main(void)
{
    char** strings;
    allocate(&strings, 48);
    //....does stuff with data
    destroy(&strings, 48);
}

void allocate(char ***strings, int size)
{
    *strings = (char**)malloc(size * sizeof(char*));
    if(strings == NULL)
    {
            printf("Could not allocate memory\n");
    }

    int i;
    for(i=0;i<size;i++)
    {
            (*strings)[i] = (char*)malloc(MAX_STRING_LEN * sizeof(char));
            if(strings == NULL)
            {
                    printf("Could not allocate memory\n");
            }
    }
}



void destroy(char ***strings, int size)
{
    int j;
    for(j=0;j<size;j++)
    {
            free(strings[j]);
    }
    free(strings);
}

您忘记了在destroy函数中取消引用strings指针:

void destroy(char ***strings, int size)
{
    int j;
    for(j=0;j<size;j++)
    {
            free( (*strings)[j] );
    }
    free(*strings);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM