2

I more or less have an idea, but I'm not sure if I've even got the right idea and I was hoping maybe I was just missing something obvious. Basically, I have and array of strings (C strings, so basically an array of pointers to character arrays) like so:

char **words;

Which I don't know how many words I'll have in the end. As I parse the string, I want to be able to resize the array, add a pointer to the word, and move on to the next word then repeat.

The only way I can think of is to maybe start with a reasonable number and realloc every time I hit the end of the array, but I'm not entirely sure that works. Like I want to be able to access words[0], words[1], etc. If I had char **words[10] and called

realloc(words, n+4)  //assuming this is correct since pointers are 4 bytes

once I hit the end of the array, if I did words[11] = new word, is that even valid?

2
  • Maybe you meant :realloc(words, n*4); ? BTW: sizeof is your friend: ptr = realloc(words, n * sizeof *words); Don't forget to use the return value from realloc() ... Commented Jan 17, 2013 at 0:46
  • Whoops, yes I meant times 4 XD I guess I typed that up a bit fast. But thanks, just wanted to make sure I had the right idea. Commented Jan 17, 2013 at 1:08

3 Answers 3

3

Keep track of your array size:

size_t arr_size = 10;

And give it an initial chunk of memory:

char **words = malloc( arr_size * sizeof(char*) );

Once you have filled all positions, you may want to double the array size:

size_t tailIdx = 0;

while( ... ) {
    if( tailIdx >= arr_size ) {
        char **newWords;
        arr_size *= 2;
        newWords = realloc(words, arr_size * sizeof(char*) );
        if( newWords == NULL ) { some_error() };
        words = newWords;
    }
    words[tailIdx++] = get_next_word();
}

...

free(words);
Sign up to request clarification or add additional context in comments.

Comments

2

That approach is fine ,although you may want to do realloc(words, n * 2) instead. calling realloc and malloc is expensive so you want to have to reallocate as little as possible and this means you can go for longer without reallocating (and possibly copying data). This is how most buffers are implemented to amortize allocation and copy costs. So just double the size of your buffer every time you run out of space.

Comments

2

You are probably going to want to allocate multiple blocks of memory. One for words, which will contain the array of pointers. And then another block for each word, which will be pointed to by elements in the words array.

Adding elements then involves realloc()ing the words array and then allocating new memory blocks for each new word.

Be careful how you write your clean up code. You'll need to be sure to free up all those blocks.

1 Comment

+1 for mentioning the allocation for the space of strings, I just assumed the asker knew that already.

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.