简体   繁体   English

如何释放 char 指针的二维数组

[英]How to deallocate a 2D array of char pointers

I have a 2D array like so:我有一个像这样的二维数组:

    char * array[2][2];

I wanted it to store char * , so I tried to malloc() using:我希望它存储char * ,所以我尝试malloc()使用:

    for(i=0:i<2;i++){
        for(j=0:j<2;j++)
        {
            array[i][j]=(char*)malloc(sizeof(char  *)*2);
        }
    }

After that, I stored some elements of char * in the 2D array, and the 2D array can print them out, which is fine.之后我把char *的一些元素存入二维数组,二维数组可以打印出来,就可以了。

I then tried to deallocate my 2D array using:然后我尝试使用以下方法解除分配我的二维数组:

    for(i=0:i<2;i++){

        for(j=0:j<2;j++){
         
            free(array[i][j]);
        }
    }

When I run the program, I get a "free(): invalid pointer" message.当我运行该程序时,我收到“free(): invalid pointer”消息。

I tried using Valgrind, which reports that all my allocs and frees are accounted for.我尝试使用 Valgrind,它报告我的所有分配和释放都已考虑在内。 However it shows that I've "definitely lost" some bytes.然而,它表明我“肯定丢失”了一些字节。

Any views or thoughts will help.任何观点或想法都会有所帮助。

Here's a sample code to demonstrate what I'm talking about:这是一个示例代码来演示我在说什么:

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

int main()
{   
    char * r0c0 = " hello ";
    char * r0c1 = " bye ";
    char * r1c0 = " this ";
    char * r1c1 = " is me ";
    
    int i=0;
    int j=0;
    for(i=0;i<2; i++)
    {
        for(j=0;j<2;j++)
        {
            *(*(array+i)+j) = (char *)malloc(sizeof(char *)*2);
        }
    }

    array[0][0]= r0c0;
    array[0][1]= r0c1;
    array[1][0]= r1c0;
    array[1][1]= r1c1;
    
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            printf("%s\t",array[i][j]);
        }
        printf("\n");
    }

    for(i=0;i<2; i++)
    {
        for(j=1;j>=0;j--)
        {
            free(*(*(array+i)+j));
        }
    }
     
    return EXIT_SUCCESS;
}

How to deallocate a 2D array of char pointers (?)如何释放 char 指针的二维数组(?)

OP is de-allocating OK, yet not allocating and using the pointers well. OP 正在取消分配 OK,但没有很好地分配和使用指针。


array[i][j]=(char*)malloc(sizeof(char *)*2); is incorrect.是不正确的。 That allocates memory for 2 pointers, yet array[i][j] needs an allocation for so many char .这为 2 个指针分配了 memory,但是array[i][j]需要分配这么多char

First, allocate enough for the strings:首先,为字符串分配足够的空间:

//  *(*(array+i)+j) = (char *)malloc(sizeof(char *)*2);
#define STRING_SIZE_MAX 7   // size of longest string to get copied for this ap.
*(*(array+i)+j) = malloc(STRING_SIZE_MAX);

array[0][0]= r0c0; writes over the data just allocated, losing the original pointer and contributing the the error seen when attempting to free.覆盖刚刚分配的数据,丢失原始指针并导致在尝试释放时出现错误。

Instead, copy the contents of a string:相反,复制字符串的内容:

// array[0][0]= r0c0;
strcpy(array[0][0], r0c0);

Better: use the non-standard, but commonly available strdup() to allocate to the right size per string and copy the contents of the string.更好:使用非标准但常用的strdup()为每个字符串分配正确的大小并复制字符串的内容。 Otherwise, easy enough to write your own my_strdup() .否则,很容易编写您自己的my_strdup()

 // int i=0;
  // int j=0;
  // for(i=0;i<2; i++) {
  //  for(j=0;j<2;j++) {
  //      *(*(array+i)+j) = (char *)malloc(sizeof(char *)*2);
  //  }
  //}

  // array[0][0]= r0c0;
  // array[0][1] = r0c1;
  // array[1][0]= r1c0;
  // array[1][1]= r1c1;

  array[0][0] = strdup(r0c0);
  array[0][1] = strdup(r0c1);
  array[1][0] = strdup(r1c0);
  array[1][1] = strdup(r1c1);

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

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