Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 内存释放?_C_Malloc_Free - Fatal编程技术网

C 内存释放?

C 内存释放?,c,malloc,free,C,Malloc,Free,我创建了一个从字符串中删除额外空格的程序 void removeDuplicateSpaces(char **c){ //a-b---c char *a=*c; char *b=malloc(sizeof(char)*strlen(*c)); <-- allocation int i=0,nf=0,space=0; for(;a[i]!='\0';i++){ if(a[i] != ' '){ //a-b-

我创建了一个从字符串中删除额外空格的程序

void removeDuplicateSpaces(char **c){  //a-b---c
    char *a=*c;
    char *b=malloc(sizeof(char)*strlen(*c));  <-- allocation
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                b[nf]=a[i];
                nf++;
                space=0;
            }else{
                b[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                b[nf]=' ';
                nf++;
            }
        }
    }
    b[i]='\0';
    *c=b;
    }

int main(void) {
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    removeDuplicateSpaces(&a); //function prototype can't be changed.
    printf("%s",a);     // ? where to deallocate.
    return 0;
}
给出了上述代码。编写一个函数
removeDuplicateSpaces
,以删除给定字符串中的多余空格

例如:(“-”表示清晰的空格)


您可以建议以下解决方案。将函数的返回类型设为
char*
,它将返回
b
所指的分配内存。然后更改函数的调用,如下面更新的代码所示。您可以在
printf
之后
main()
中释放
内存

#include<stdio.h>

char *removeDuplicateSpaces(const char **c){  //a-b---c
    char *a=*c;
    char *b=malloc(sizeof(a));  <-- allocation
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                b[nf]=a[i];
                nf++;
                space=0;
            }else{
                b[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                b[nf]=' ';
                nf++;
            }
        }
    }
    b[i]='\0';
    *c=b;
    return b;
    }

int main(void) {
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    char *c;
    c=removeDuplicateSpaces(&a);
    printf("%s",a);     // ? where to deallocate.a
    free(c);
    return 0;
}
#包括
char*removeDuplicateSpaces(const char**c){//a-b--c
字符*a=*c;
char*b=malloc(sizeof(a));1){
b[nf]=a[i];
nf++;
空间=0;
}否则{
b[nf]=a[i];
nf++;
}
}否则{
空间++;
if(空格==1&&i!=0){
b[nf]='';
nf++;
}
}
}
b[i]='\0';
*c=b;
返回b;
}
内部主(空){
char*a=“Arista正在从ISM Dhanbad招聘”;
char*c;
c=移除的重复空间(&a);
printf(“%s”,a);/?在何处解除分配。a
免费(c);
返回0;
}

最好不要从RemovedUpplicateSpace函数返回一些已分配的字符串。相反,将其修改为对已分配的缓冲区进行操作,这样您就可以确切地知道何时可以释放分配的内存

大概是这样的:

char *a="    Arista    is     hiring    from   ISM   Dhanbad";
char *b = (char *)malloc(sizeof(a)); // for sure result string will be less or equal to origin
removeDuplicateSpaces(&a, b);
printf("%s",b);
free(b);
removeDuplicateSpaces
中,您不需要分配任何内容

编辑: 试试这个


这里有另一种方法

char* removeDuplicateSpaces( char const * src )  // show that input string is read-only
{
  char* strnospaces = calloc( 1, strlen(src)+1 );// string is filled with \0's
  for (char *t = strnospaces, *s = src; *s; )    // copy until \0
    if (!isspace(*s)) *t++=*s++; else s++;       // copy only if not space
  return strnospaces;  
}

您确实需要释放的返回值。您不需要的原始指针<代码>免费(a)
printf()
之后,但在
return
语句之前。您应该知道这个函数有未定义的行为。您只分配相当于指针大小的空间,而不是原始字符串<代码>字符*b=malloc(sizeof(a))分配指针的大小,而不是输入字符串的长度。我的意思是您需要先修复原始函数,这样它就不会有未定义的行为。我已经告诉过你在哪里处理(大小不正确的)malloced数据:在关闭
printf()
之后,但在返回之前,
free(a)。将其从
sizeof(a)
更改为
sizeof(*a)
不是解决方案。这将为一个字符分配空间,而不是为终止零分配字符串长度+1的空间。你知道C语言中以零结尾的字符串是什么吗?如何找到它的长度?仍然缺少终止零的空间,我希望您认识到这个函数的原型相当糟糕。它接受指向常量字符指针的指针。这意味着传递非常量字符指针的地址在技术上是无效的,因为它删除了内部常量限定符。此外,您不能通过使
main()
s变量
a
const来“修复”这个问题,因为如果没有强制转换,您就不能
free()
它。简言之,此操作的输入参数键入错误。我不知道这个函数解决的最初问题是什么,但对我来说,它闻起来像个XY问题。祝你好运。@WhozCraig发布了实际问题。在访谈中被问到。函数原型不能更改。@根据您发布的更新,原型必须更改(尽管不一定要符合此答案),因为原始问题需要一个
char*
,而您的函数需要一个
const char**
。它们是完全不同的类型,不兼容。不能更改函数原型。然后,您必须修改算法,不分配新字符串,而是更改现有字符串。它只是一个字符数组,您可以通过向左移动值并将“\0”放在新的一端来“压缩”它。请提供代码。我想不起来了。我的意思是我不知道如何修改(如果可能的话)文本字符串?你在想斯特伦(a)可能吗?@cacho只是复制粘贴了它,没有注意到:)ty。WhozCraig已经在原始帖子的评论中指出了这一点。这是我在采访中提出的。但我不允许更改
void
返回类型:(啊,好吧!那么我建议您使用char[]作为输入,而不是指针,这样可以更清楚地看到您正在修改字符串,即
void removeDuplicateSpaces(char src[])
如果输入是数组格式,它会修改原始字符串吗?+1我喜欢这个解决方案。如果你将数组传递给函数,你可以修改它,如果你正在修改内容,你不应该传递指向字符串文本的指针。字符串文本是只读的。例如
char*p=“mytext”;
otoh
char p[]=“mytext”;
不是只读的
char *a="    Arista    is     hiring    from   ISM   Dhanbad";
char *b = (char *)malloc(sizeof(a)); // for sure result string will be less or equal to origin
removeDuplicateSpaces(&a, b);
printf("%s",b);
free(b);
void removeDuplicateSpaces(const char **c){  //a-b---c
    char *a=*c;
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                a[nf]=a[i];
                nf++;
                space=0;
            }else{
                a[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                a[nf]=' ';
                nf++;
            }
        }
    }
    a[nf]='\0';
}

int main()
{
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    char *b = (char *)malloc(strlen(a)+1);
    strcpy(b, a);
    removeDuplicateSpaces(&b); //function prototype can't be changed.
    printf("%s",b);
    free(b);
    return 0;
}
char* removeDuplicateSpaces( char const * src )  // show that input string is read-only
{
  char* strnospaces = calloc( 1, strlen(src)+1 );// string is filled with \0's
  for (char *t = strnospaces, *s = src; *s; )    // copy until \0
    if (!isspace(*s)) *t++=*s++; else s++;       // copy only if not space
  return strnospaces;  
}