繁体   English   中英

C内存释放?

[英]C memory deallocation?

我创建了一个从字符串中删除多余空格的程序。

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()函数中分配。 如果我的后面添加自由发言printfmain那么它使我的程序崩溃( signal 6 abort )。 那么正确的方法是什么?


原始问题

#include<stdio.h>
main()
{
    char *foo = "    Arista    is     hiring    from   ISM   Dhanbad";
    void removeDuplicateSpaces(foo);
    printf("%s\n", foo);
}

上面的代码给出了。 编写函数removeDuplicateSpaces以删除给定字符串中的额外空格。

例如:(' - '表示空格清晰)

Input String : (without quotes)
“—-Arista——is—-hiring—-from-ISM–Dhanbad”
Output String :
“Arista-is-hiring-from-ISM-Dhanbad”

最好不要从removeDuplicateSpaces函数返回一些已分配的字符串。 而是修改它以对已经分配的缓冲区进行操作,然后您将确切地知道何时可以释放您分配的内存。

像这样的东西:

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您不需要分配任何内容。

编辑:试试这个

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;  
}

您会建议以下解决方案。 使函数的返回类型为char* ,它将返回b指向的已分配内存。 然后更改函数的调用,如下面更新的代码所示。 然后你可以在printf之后free 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;
}

暂无
暂无

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

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