C dynamically growing array

前端 未结 7 1993
闹比i
闹比i 2020-11-22 07:57

I have a program that reads a \"raw\" list of in-game entities, and I intend to make an array holding an index number (int) of an indeterminate number of entities, for proce

7条回答
  •  囚心锁ツ
    2020-11-22 08:38

    As with everything that seems scarier at first than it was later, the best way to get over the initial fear is to immerse yourself into the discomfort of the unknown! It is at times like that which we learn the most, after all.

    Unfortunately, there are limitations. While you're still learning to use a function, you shouldn't assume the role of a teacher, for example. I often read answers from those who seemingly don't know how to use realloc (i.e. the currently accepted answer!) telling others how to use it incorrectly, occasionally under the guise that they've omitted error handling, even though this is a common pitfall which needs mention. Here's an answer explaining how to use realloc correctly. Take note that the answer is storing the return value into a different variable in order to perform error checking.

    Every time you call a function, and every time you use an array, you are using a pointer. The conversions are occurring implicitly, which if anything should be even scarier, as it's the things we don't see which often cause the most problems. For example, memory leaks...

    Array operators are pointer operators. array[x] is really a shortcut for *(array + x), which can be broken down into: * and (array + x). It's most likely that the * is what confuses you. We can further eliminate the addition from the problem by assuming x to be 0, thus, array[0] becomes *array because adding 0 won't change the value...

    ... and thus we can see that *array is equivalent to array[0]. You can use one where you want to use the other, and vice versa. Array operators are pointer operators.

    malloc, realloc and friends don't invent the concept of a pointer which you've been using all along; they merely use this to implement some other feature, which is a different form of storage duration, most suitable when you desire drastic, dynamic changes in size.

    It is a shame that the currently accepted answer also goes against the grain of some other very well-founded advice on StackOverflow, and at the same time, misses an opportunity to introduce a little-known feature which shines for exactly this usecase: flexible array members! That's actually a pretty broken answer... :(

    When you define your struct, declare your array at the end of the structure, without any upper bound. For example:

    struct int_list {
        size_t size;
        int value[];
    };
    

    This will allow you to unite your array of int into the same allocation as your count, and having them bound like this can be very handy!

    sizeof (struct int_list) will act as though value has a size of 0, so it'll tell you the size of the structure with an empty list. You still need to add to the size passed to realloc to specify the size of your list.

    Another handy tip is to remember that realloc(NULL, x) is equivalent to malloc(x), and we can use this to simplify our code. For example:

    int push_back(struct int_list **fubar, int value) {
        size_t x = *fubar ? fubar[0]->size : 0
             , y = x + 1;
    
        if ((x & y) == 0) {
            void *temp = realloc(*fubar, sizeof **fubar
                                       + (x + y) * sizeof fubar[0]->value[0]);
            if (!temp) { return 1; }
            *fubar = temp; // or, if you like, `fubar[0] = temp;`
        }
    
        fubar[0]->value[x] = value;
        fubar[0]->size = y;
        return 0;
    }
    
    struct int_list *array = NULL;
    

    The reason I chose to use struct int_list ** as the first argument may not seem immediately obvious, but if you think about the second argument, any changes made to value from within push_back would not be visible to the function we're calling from, right? The same goes for the first argument, and we need to be able to modify our array, not just here but possibly also in any other function/s we pass it to...

    array starts off pointing at nothing; it is an empty list. Initialising it is the same as adding to it. For example:

    struct int_list *array = NULL;
    if (!push_back(&array, 42)) {
        // success!
    }
    

    P.S. Remember to free(array); when you're done with it!

提交回复
热议问题