且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何释放一个字符指针数组?

更新时间:2023-11-17 17:46:28

问题是(*array)++不会给您分配的下一个指针,因此您不能释放它.您的免费例程应为:

The problem is that (*array)++ doesn't give you the next pointer you allocated, so you can't free it. Your free routine should be:

void freeargpointer(char** array)
{
    int i;

    for ( i = 0; array[i]; i++ )
        free( array[i] );

    free( array );
}

或者类似地,

void freeargpointer(char** array)
{
    char **a;

    for ( a = array; *a; a++ )
        free( *a );

    free( array );
}

注意:我删除了count参数,因为它是不必要的.

NOTE: I removed the count argument since it is unnecessary.