Thread: growing an array the right way

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    8

    growing an array the right way

    I kinda need someone that knows what they are doing with C to pick this apart, possibly show how you would do it? (It does run already but I do have some doubts).

    I am attempting to practice dynamically increasing an array, and I am unsure if the last realloc is really needed (after the loop finishes). My intention is to shrink it down to its actual size.

    I also used calloc for the first allocation because I didn't want to bother inserting the null terminator later.

    Also, I do not know if strlen is the best way to get the current size of what has been allocated already when calling realloc. I was thinking that sizeof would work work, but I think that is actually not the right size to use there.

    Another doubt that I have is the index in the loop... I only added that to set
    Code:
    buffer[index] = *words;
    But what I really wanted to do was
    Code:
    *buffer = *words;
    which did not work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define CHUNK 32
    
    
    
    
    int main() {
        char *words = "At vero eos et accusamus et iusto odio dignissimos ducimus qui"
                      "blanditiis praesentium voluptatum deleniti atque corrupti quos"
                      "dolores et quas molestias excepturi sint occaecati cupiditate"
                      "non provident, similique sunt in culpa qui officia deserunt"
                      "mollitia animi, id est laborum et dolorum fuga. Et harum quidem"
                      "rerum facilis est et expedita distinctio. Nam libero tempore,"
                      "cum soluta nobis est eligendi optio cumque nihil impedit quo"
                      "minus id quod maxime placeat facere possimus, omnis voluptas"
                      "assumenda est, omnis dolor repellendus. Temporibus autem"
                      "quibusdam et aut officiis debitis aut rerum necessitatibus"
                      "saepe eveniet ut et voluptates repudiandae sint et molestiae"
                      "non recusandae. Itaque earum rerum hic tenetur a sapiente"
                      "delectus, ut aut reiciendis voluptatibus maiores alias"
                      "consequatur aut perferendis doloribus asperiores repellat.";
    
    
        char *buffer = calloc(CHUNK,  sizeof(char));
        int index;
        for (index = 0; *words != '\0'; words++, index++) {
            if ((index + 1) % CHUNK != 0) {
                char *tmpbuff = realloc(buffer, strlen(buffer) + CHUNK);
                if (!(tmpbuff)) {
                    printf("Failed allocation, exiting...");
                    free(buffer);
                    exit(1);
                }
                buffer = tmpbuff;
            }
            buffer[index] = *words;
        }
    
    
        char *tmp = realloc(buffer, strlen(buffer));
        buffer = tmp;
    
    
        printf("%s\n", buffer);
        free(buffer);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,422
    Since the calloc is only for the first allocation you cannot be sure of zero-termination with the reallocs. Best to store the '\0' yourself.

    To determine the currently allocated size you cannot use strlen or sizeof. There is no standard way to do this. Instead, just keep track of the size yourself.

    Since sizeof(char) is defined to always be 1, you may as well just say 1.

    It is very old-fashioned to define a for-loop index variable outside of the for-loop head unless it is being used after the loop for some purpose.

    In your case, the final realloc is pointless as it could only save you at most 31 bytes, although it can be useful to realloc downwards as this frees up memory for other allocations by the same program.

    Coincidentally, your text, including the '\0', is divisible by 32.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define CHUNK 32
     
    void *my_realloc(void *buffer, size_t size) {
        char *tmp = realloc(buffer, size);
        if (!tmp) {
            perror("realloc");
            free(buffer);
            exit(1);
        }
        return tmp;
    }
     
    int main() {
        const char *words =
            "At vero eos et accusamus et iusto odio dignissimos ducimus qui"
            "blanditiis praesentium voluptatum deleniti atque corrupti quos"
            "dolores et quas molestias excepturi sint occaecati cupiditate"
            "non provident, similique sunt in culpa qui officia deserunt"
            "mollitia animi, id est laborum et dolorum fuga. Et harum quidem"
            "rerum facilis est et expedita distinctio. Nam libero tempore,"
            "cum soluta nobis est eligendi optio cumque nihil impedit quo"
            "minus id quod maxime placeat facere possimus, omnis voluptas"
            "assumenda est, omnis dolor repellendus. Temporibus autem"
            "quibusdam et aut officiis debitis aut rerum necessitatibus"
            "saepe eveniet ut et voluptates repudiandae sint et molestiae"
            "non recusandae. Itaque earum rerum hic tenetur a sapiente"
            "delectus, ut aut reiciendis voluptatibus maiores alias"
            "consequatur aut perferendis doloribus asperiores repellat.";
     
        int bufsize = CHUNK;
        char *buffer = my_realloc(NULL, bufsize), *b = buffer;
     
        for (const char *w = words; (*b++ = *w++); )
            if (b - buffer == bufsize)
                buffer = my_realloc(buffer, bufsize += CHUNK);
     
        printf("%s\n", buffer);
     
        printf("bufsize: %5d\n", bufsize);
        printf("used   : %5d\n", (int)(b - buffer));
     
        free(buffer);
     
        return 0;
    }
    A long time ago being crazy meant something. - Charles Manson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,308
    Do you know the range of text lengths?

    For example, you wouldn't want to try and store 1MB of text, by creeping up on it 32 bytes at a time.

    If you don't know the length in advance, the strategy is to double the size on each realloc, not add a constant.
    realloc is fairly expensive, especially when it decides to move the block to some better location in memory.

    If you do know the length, then just malloc and be done.

    > I am unsure if the last realloc is really needed (after the loop finishes). My intention is to shrink it down to its actual size.
    Well the first thing to note is your attempt to reduce the size manages to eliminate the \0 from the end of the string.
    It should have been strlen(buffer)+1

    It's only worth doing if you intend to keep the buffer for a long time.
    If you're just going to do some immediate work, and then free it, trimming the size isn't worth it.

    On the other hand, if you go for the doubling strategy, trimming the length might be worth doing.
    Particularly when your step sizes get into the MB range for example.


    Also, you want to keep separate track of the length yourself (as john.c does). Calling strlen() on an ever expanding buffer is just a waste of effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2022
    Posts
    8
    Thank you john.c and Salem. This is great info and answered all of my questions!

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    8
    Ah sorry... I have one question that I hadn't noticed when I first looked at your answer...

    Code:
    char *buffer = my_realloc(NULL, bufsize), *b = buffer;
    
    How is it possible/valid to use *b = buffer there?

    I would have thought buffer would be undefined and that *b would need a type with its declaration.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,308
    It's just the slightly shorter way of writing
    Code:
    char *buffer = my_realloc(NULL, bufsize);
    char *b = buffer;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch concept and growing array ?
    By OM42 in forum C Programming
    Replies: 8
    Last Post: 06-03-2022, 05:01 AM
  2. Memory program size is growing
    By Kempelen in forum C Programming
    Replies: 5
    Last Post: 01-26-2013, 12:41 PM
  3. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  4. Growing a Binary Tree
    By hdragon in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2007, 02:41 AM
  5. DCOM Server - Memory keeps growing
    By freestyle in forum Windows Programming
    Replies: 5
    Last Post: 10-25-2002, 11:00 AM

Tags for this Thread