简体   繁体   中英

C memory deallocation?

I created a program for removing extra spaces from string.

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

Working demo

It is working fine. But the issue is where should i deallocate memory, allocated in removeDuplicateSpaces() function. If i add free statement after the printf in main then it make my program to crash( signal 6 abort ). So what is the correct way ?


Original Problem

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

Above code was given. Write a function removeDuplicateSpaces so as to remove the extra spaces in the given string.

For Example : ( '-' denotes spaces for clarity)

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

It's better not to return some allocated string from your removeDuplicateSpaces function. Instead modify it to operate on already allocated buffer, then you will know exactly when memory you allocated can be freed.

Something like this:

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

and in removeDuplicateSpaces you don't need to allocate anything then.

EDIT: Try this

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

here is an alternative way to do it

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

You would suggest the following solution. Make return type of your function to be char* which will return the allocated memory pointed by b . Then change invocation of the function like shown in the following updated code. You can free the memory in main() after printf then.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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