Thread: Dynamic arrays initialization

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    22

    Dynamic arrays initialization

    I want to create arrays within structures whose sizes I do not know untill I finish the loop.
    I thought I needed to reallocate the size after every loop iteration is done so that it would have enough memory for the next iteration,
    but then I get an error, whereas it seems to be okay when I do not reallocate size.

    Can you please tell me if I need to reallocate memory here after every loop iteration?
    And what is generally the rule to do with initializing arrays whose size and members we don't know in advance?

    Code:
    
    for(co_b=0;  co_b < aaa->no_b; co_b++) {
    
         int i=0;
    
         if ((aaa->bbb[co_b].ce = (double *)malloc(sizeof(double)*2)) == NULL) {
            fprintf(stdout, "/nOut of Memory");
            return EXIT_FAILURE;
        }
    
        for(co_x=0; co_x < aaa->bbb[co_b].no_x;  co_x++) {
    
        aaa->bbb[co_b].ce[i]=5*x;
        aaa->bbb[co_b].ce[i+1]=10*x;
    
        i=i+2;
    
    // if I add this part below I get an error saying:
    // size realloc(): invalid next size:
    // and then later core dumped
    
    
        if ((aaa->bbb[co_b].ce = (double *)realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+1)) ) == NULL) {
            fprintf(stdout, "\nOut of Memory!");
            exit (EXIT_FAILURE);
        }
    
    
    
      }
    
    }
    Last edited by Layla_E; 03-19-2018 at 05:57 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly, e.g.,
    Code:
    int i=0;
    
    for (co_b=0; co_b < aaa->no_b; co_b++) {
    
        for (co_x=0; co_x < aaa->bbb[co_b].no_x; co_x++) {
    
            if ((aaa->bbb[co_b].ce = (double *)malloc(sizeof(double)*2)) == NULL) {
                fprintf(stdout, "/nOut of Memory");
                return EXIT_FAILURE;
            }
    
            aaa->bbb[co_b].ce[i]=5*x;
            aaa->bbb[co_b].ce[i+1]=10*x;
    
            i=i+2;
    
            // if I add this part below I get an error saying:
            // size realloc(): invalid next size:
            // and then later core dumped
    
            if ((aaa->bbb[co_b].ce = (double *)realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+1)) ) == NULL) {
                fprintf(stdout, "\nOut of Memory!");
                exit (EXIT_FAILURE);
            }
    
        }
    
    }
    The problem appears to be that you are always allocating space for aaa->bbb[co_b].ce within the inner loop using malloc. Hence, your realloc is pointless: you will overwrite it on subsequent iterations. What you probably should do is something like this:
    Code:
    int i=0;
    
    for (co_b=0; co_b < aaa->no_b; co_b++) {
    
        if ((aaa->bbb[co_b].ce = (double *)malloc(sizeof(double)*2)) == NULL) {
            fprintf(stdout, "/nOut of Memory");
            return EXIT_FAILURE;
        }
    
        for (co_x=0; co_x < aaa->bbb[co_b].no_x; co_x++) {
    
            aaa->bbb[co_b].ce[i]=5*x;
            aaa->bbb[co_b].ce[i+1]=10*x;
    
            i=i+2;
    
            // if I add this part below I get an error saying:
            // size realloc(): invalid next size:
            // and then later core dumped
    
            if ((aaa->bbb[co_b].ce = (double *)realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+1)) ) == NULL) {
                fprintf(stdout, "\nOut of Memory!");
                exit (EXIT_FAILURE);
            }
    
        }
    
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    22
    Hi, sorry that was my mistake while simplifying the code and copying it here, thanks, malloc was actually originally in the outer loop, not the inner so that wasn't the problem. I edited the question now.
    Last edited by Layla_E; 03-19-2018 at 05:59 AM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It's best not to use an initial malloc when you are reallocing for every new record. Instead, start the pointer at NULL and call realloc when you know you need the extra space.
    Code:
    for(co_b=0;  co_b < aaa->no_b; co_b++) {
     
        int i=0;
        aaa->bbb[co_b].ce = NULL;
    
        for(co_x=0; co_x < aaa->bbb[co_b].no_x;  co_x++) {
     
            if ((aaa->bbb[co_b].ce = realloc(aaa->bbb[co_b].ce, sizeof(double) * (i+2)) ) == NULL) {
                perror("realloc");
                exit (EXIT_FAILURE);
            }
     
            aaa->bbb[co_b].ce[i] = 5 * x;
            aaa->bbb[co_b].ce[i+1] = 10 * x;
    
            i += 2;
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    22
    Thank you, this works now!

    Do you know perhaps why it seemed as if it works fine even without memory reallocation, i.e. the code as it is in my question with malloc, but without realloc?
    I want to be careful to avoid it since it has happened that the structures and arrays get filled with values, but only later I find that the memory wasn't properly allocated when using them further.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by Layla_E View Post
    Do you know perhaps why it seemed as if it works fine even without memory reallocation, i.e. the code as it is in my question with malloc, but without realloc?
    I don't know in detail what's going on but the basic idea is that there is usually more memory assigned to your process by the OS than you have actually requested from the allocator (malloc/calloc/realloc).

    Presumably the realloc calls cause the allocator to notice the problem by recognizing that it's housekeeping data has been overwritten.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initialization in multi dimensional arrays
    By narendrav in forum C Programming
    Replies: 41
    Last Post: 03-17-2014, 11:06 PM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Dynamic arrays
    By 031003b in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2007, 10:09 AM
  5. Default Initialization of Aggregates / Arrays
    By Tonto in forum C++ Programming
    Replies: 8
    Last Post: 11-20-2006, 02:14 AM

Tags for this Thread