C 无自由字符串的自由双数组字符

C 无自由字符串的自由双数组字符,c,arrays,C,Arrays,我正在寻找一个不含自由字符串的双字符数组,下面是我的代码: char **rea_scd_array(char **src, int add) { char **ret; int i; i = 0; while (src[i] != '\0') i++; ret = (char**) malloc(sizeof(char*) * (i + add + 1)); ret[i + add] = '\0'; while (i

我正在寻找一个不含自由字符串的双字符数组,下面是我的代码:

char **rea_scd_array(char **src, int add)
{
    char **ret;
    int  i;

    i = 0;
    while (src[i] != '\0')
        i++;
    ret = (char**) malloc(sizeof(char*) * (i + add + 1));
    ret[i + add] = '\0';
    while (i >= 0)
    {
        ret[i] = &src[i][0];
        i--;
    }
    free(&src);
    return (ret);
}
我的想法是添加新的维度来存储其中的字符串,而不使用数组的realloc all表,并复制所有旧内容

我的代码不起作用了,我明白了

`"malloc: *** error for object 0x7fff5fbff9e8: pointer being freed was not allocated
 set a breakpoint in malloc_error_break to debug"`
我只是想释放存储字符串指针的第二个数组维度


有什么想法吗?

您首先尝试释放从未分配内存的
src
。您可以释放动态分配给堆的内存(通常通过
malloc
)。

如果您事先不知道大小,并且不想
realloc
,请使用单链接列表:

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

typedef struct node {
    char *data;
    struct node *next;
} t_node;

extern char *strdup(const char *);

t_node *insert(t_node *node, char *s)
{
    t_node *new = malloc(sizeof(*new));

    if (new == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    new->data = strdup(s);
    if (new->data == NULL) {
        perror("strdup");
        exit(EXIT_FAILURE);
    }
    new->next = NULL;
    if (node != NULL) node->next = new;
    return new;
}

int main(void)
{
    t_node *temp, *first, *node = NULL;

    node = first = insert(node, "First");
    node = insert(node, "Second");
    node = insert(node, "Third");
    node = insert(node, "Last");
    node = first;
    while (node) {
        printf("%s\n", node->data);
        temp = node->next;
        free(node->data);
        free(node);
        node = temp;
    }
    return 0;
}
#包括
#包括
类型定义结构节点{
字符*数据;
结构节点*下一步;
}t_节点;
外部字符*strdup(常量字符*);
t_节点*插入(t_节点*节点,字符*s)
{
t_node*new=malloc(sizeof(*new));
if(new==NULL){
佩罗尔(“马洛克”);
退出(退出失败);
}
新建->数据=strdup;
如果(新建->数据==NULL){
perror(“strdup”);
退出(退出失败);
}
新建->下一步=空;
如果(node!=NULL)node->next=new;
归还新的;
}
内部主(空)
{
t_node*temp,*first,*node=NULL;
节点=第一个=插入(节点,“第一个”);
节点=插入(节点,“第二”);
节点=插入(节点,“第三”);
节点=插入(节点,“最后”);
节点=第一;
while(节点){
printf(“%s\n”,节点->数据);
temp=节点->下一步;
自由(节点->数据);
自由(节点);
节点=温度;
}
返回0;
}

您的实现迫使您在数组末尾添加一个
NULL
指针

以下几行代码让我怀疑这是您的本意:

while (src[i] != '\0')
ret[i + add] = '\0';
如果它是
NULL
而不是
'\0'
,那么我会不这么认为(尽管它们本质上是相同的)

除此之外,使用大于1的
add
将得到一个包含一些“垃圾指针”的数组

无论如何,此时我们有两个选项(不包括
realloc
):

  • 使用
    NULL
    指针指示数组的结尾
  • 使用
    len
    变量指示阵列的长度
  • 第二种选择效率更高,因此您的境况会更好

    选项1:

    char**rea\u scd\u数组(char**src,int-add)
    {
    字符**ret;
    int i;
    int len=0;
    while(src[len]!=NULL)
    len++;
    ret=(char**)malloc(sizeof(char*)*(len+add+1));
    
    对于(i=0;i英语很好,但缩进很糟糕。。你的英语还不错。:)。样式提示:1)将编辑器设置为使用四个空格而不是制表符进行缩进。制表符会自找麻烦。2)始终使用
    {}
    用于
    if
    while
    的主体,即使是单行。3)不要强制转换
    malloc()的返回值
    。我的想法是不要realloc所有数组,realloc是重的。我使用strsplit创建的双字符数组调用此函数。因此分配了数组:)我的空闲是坏的,但不理解why@insidelulz您确定不仅要将内存分配给
    char**src
    ,而且要将内存分配给这些指针poi中的每个字符串吗是的,我的src数组是由strsplit生成的
    char **rea_scd_array(char **src, int add)
    {
        char **ret;
        int  i;
        int  len = 0;
        while (src[len] != NULL)
            len++;
        ret = (char**) malloc(sizeof(char*) * (len + add + 1));
        for (i=0; i<len; i++)
            ret[i] = src[i];
        for (; i<len+add+1; i++)
            ret[i] = NULL; // necessary in order for this function to work correctly
        free(src);
        return ret;
    }
    
    char **rea_scd_array(char **src, int len, int add)
    {
        char **ret;
        int  i;
        ret = (char**) malloc(sizeof(char*) * (len + add));
        for (i=0; i<len; i++)
            ret[i] = src[i];
        for (; i<len+add; i++)
            ret[i] = NULL; // necessary for other functions that are using this array
        free(src);
        return ret;
    }