Realloc problems with sturcture array inside structure

This is a discussion on Realloc problems with sturcture array inside structure within the C Programming forums, part of the General Programming Boards category; I have searched the forum and cant find the answer so i thought that i would ask. I am using ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    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;

    }BRANCH;


    typedef struct{

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

    }TREE;

    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
    Location
    Waterloo, Texas
    Posts
    5,643
    >> 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.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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
    Code:
    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.

    Example
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:01 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08: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, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21