0

I have to write a function that adds the given string to array of strings.

char **add_string(char **array, const char *string)
{
array = realloc(array, (sizeof(string)) * sizeof(char *));
int i = 0;
while (array[i] != NULL){        
    i++;
}
array[i] = malloc((strlen(string) + 1));
strcpy(array[i], string);   
array[i + 1] = NULL;
return array;
}

At the moment I get memory leaks according to Valgrid. I didn't use malloc correctly, but I can't figure out how to fix it. I would really appreciate your help.

2
  • Using sizeof on a pointer gives you the size of the pointer and not what its pointing to, so what you are doing in the realloc call is really sizeof(char *) * sizeof(char *), which is probably not what you want. Commented Apr 14, 2015 at 11:54
  • In general you only want to use realloc when you run out of space, not as a tool to create 1-more on each iteration. In the allocation scheme you generally start with some number of pointers that should be sufficient for your needs. (e.g. 32, 64, 128, 256, ...) Even with 256 pointers, that only takes a tiny 2k of memory. Create a convenient #define MAXS 256 at the top of your code. You allocate your char **array = calloc (MAXS, sizeof *array); You then allocate each array[i] (or just assign with strdup). When (i == MAXS), you realloc MAXS * 2 * sizeof *array. Commented Apr 15, 2015 at 9:29

3 Answers 3

1

The realloc allocates a new size given its second argument but you are not increasing the size, so basically every time you call your function it allocates the same size not increasing the area allocated.

you would need to pass the current number of strings in the array to add_string and then increment it in the add_string

char** add_string(char** array, int* size, const char* string)
{
   char* newArray = realloc(array, (*size + 1) *sizeof(char*) );
   newArray[*size] = malloc(strlen(string)+1);
   strcpy(newArray[*size], string);
   *size += 1;

...
}

You should also check if realloc succeeds or not by checking the return value.

Normally the above method is not a very effective way to handle increasing sizes since you are calling realloc every time and that is time consuming. Instead you should allocate in chunks then keep track of how much of the chunk you have used up, when the chunk is used up realloc a new one.

Sign up to request clarification or add additional context in comments.

Comments

0

Your re-allocation of array is broken, it needs to search to figure out how long the array is, assuming it's always "tightly" allocated (with the last allocated element being the only one that is NULL).

Comments

0
char **add_string(char **array, const char *string){
    int i = 0;
    while (array[i] != NULL){        
        i++;//First, count the elements and find NULL element
    }
    array[i] = malloc(strlen(string) + 1);//or strdup(string);
    strcpy(array[i], string);
    char **temp;
    temp = realloc(array, (i+1+1) * sizeof(char *));
    if(temp == NULL){
        perror("realloc at add_string");
    } else {
        array = temp;
        array[i + 1] = NULL;
    }
    return array;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.