Thread: Realloc problems with sturcture array inside structure

  1. #1
    Registered User
    Join Date
    Mar 2004

    Realloc problems with sturcture array inside structure

    I have searched the forum and cant find the answer so i thought that i would ask.

    I am using C and have the following two structures:

    typedef struct{
    int origX, origY, origZ, maxLengthBool;
    float currentLength[3];
    float dirX, dirY, dirZ;
    double maxLength;


    typedef struct{

    int branchCount;
    BRANCH *branches;
    COLOUR treeColour;
    int dirBias;


    You should be able to see that there is a pointer for branch in the tree sturucture.

    I can assign memeory using calloc for the BRANCH pojnter/array

    mainTree->branches = calloc(0,sizeof(BRANCH));

    This works and creates an array of branches. But i jave a problem when i try and realloc the memory and increase the size of the array i use this code, which is part of a method/function:

    tree->branches = realloc(tree->branches, brSize*sizeof(BRANCH));

    Where brSize is the new size that i would like.

    In windows using VC++ and openGL it causes a fatal error. I know it is this section, the realloc as the program doesnt get furthur than this point.

    Can anyone suggest a solution as i am lost and dont understand the problem.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    >> calloc(0,sizeof(BRANCH));

    You're using calloc wrong. The first arg is the number of items, the second is the size of each item. Also, always check the return value when making allocations.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Yeah, but for an expanding array, it won't actually matter.
    In fact, the preferred way to start and expanding array such as this is
    mainTree->branches = NULL;
    mainTree->branchCount = 0;

    Passing NULL to realloc is the same as calling malloc.

    > tree->branches = realloc(tree->branches, brSize*sizeof(BRANCH));
    You lose rather badly if realloc fails, you've just lost your pointer to your memory you did have.
    Do it like this
    void *temp = realloc(tree->branches, brSize*sizeof(BRANCH));
    if ( temp != NULL ) {
      tree->branches = temp;
    } else {
      // some error, continue with existing tree->branches
    > the realloc as the program doesnt get furthur than this point.
    Unfortunately, this is seldom where the problem actually is. The true cause of where the problem is will likely be elsewhere in the code. This is just where the problem is noticed by the memory allocation routines.

    int *p1 = malloc( sizeof *p1 );  // an int
    p1[1] = 0;  // oops, out of bounds
    // but unlikely to cause an immediate failure
    // lots more code
    int *p2 = malloc( sizeof *p2 ); // this one crashes the machine
    // but the damage was caused by overstepping p1
    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. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 03:43 PM