0

I am curious how to allocate memory for char** when I do not know how many words I will have and what is the maximum length of words. I need to divide string (which I get as char*) on multiple tokens (words) and save separate words in char**. I understand that I can allocate size for *char as (string length+1) and for each char[i] I can allocate also (string length + 1) <- in case we have the whole sting as a one token or we have each character in the string as a separate words. So I think the code would be:

char **words = malloc((strlen(str)+1) * sizeof(char*));
int i;
for( i=0; i < strlen(str)+1; i++)
 words[i] = malloc((strlen(str)+1) * sizeof(char));

Is it correct? Can I do if more efficient?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
YohanRoth
  • 3,153
  • 4
  • 31
  • 58
  • 2
    Calculate the total size needed for the array and all of the strings, allocate it is one call to malloc and then fill in the block? – Brian Walker Sep 05 '14 at 17:17
  • Efficient in speed or space? – chux - Reinstate Monica Sep 05 '14 at 17:32
  • `str` will have a length `len`. You will get at most `len` tokens. The _sum_ of all the memory needed for the token strings will be at most `len + len*1`. So code needs to allocate `len` pointers and `len*2` for the all the tokens. This can be done with a single `malloc(len*sizeof(*words) + len*2)`. – chux - Reinstate Monica Sep 07 '14 at 03:33

2 Answers2

0

You could make it faster by running strlen() only once and saving that variable. You are also allocating way more than you need to. First find out how many words you have and allocate char **words by that, then for each words[i] allocate only enough for each word.

0

You don't need to allocate the memory for each word separately. Instead you can duplicate the whole string with strdup(str) (or malloc(strlen(str)+1) and strcpy()) and modify the copy by replacing the word delimiters with '\0' (as e. g. strtok() does).

Regarding the maximum number of words: If two back-to-back delimiters are to form an empty word between them, there can be strlen(str)+1 words; if two or more contiguous delimiter characters are considered to be a single delimiter, there can be only half as many words.

Armali
  • 18,255
  • 14
  • 57
  • 171