realloc()

This is a discussion on realloc() within the C Programming forums, part of the General Programming Boards category; Code: int build (FILE *fp, COLOR **ppcolor) { int size = 0; *ppcolor = (COLOR *) malloc (sizeof (COLOR)); while ...

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    realloc()

    Code:
    int build (FILE *fp, COLOR **ppcolor)
    {
     int size = 0;
    
     *ppcolor = (COLOR *) malloc (sizeof (COLOR));
    
     while (fread (*ppcolor + size, sizeof (COLOR), 1, fp) == 1)
     {
      size++;
    
      *ppcolor = realloc (*ppcolor, (size + 1) * sizeof (COLOR));
     } /* while */
    
     return size;
    } /* build */
    With function build() I intend to create an array of structures (of type COLOR) which starts at memory location *ppcolor. Data are read from binary file fp (full of COLOR structures).

    This works fine. I am just not satisfied with this code. Realloc is inside a loop which may take up to 1.000.000 iterations. I know realloc() may move the whole allocated memory block if there's no enough space for the new incoming elements, so this function could be moving increasingly larger blocks of memory for each iteration !!

    I hope you get my point.

    Any help appreciated !
    Last edited by PutoAmo; 03-14-2002 at 01:57 PM.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    You'd better read the whole file at once.
    Just get its size and use it in the first malloc() and fread()
    I can not remember if there is a function to directly get its size, but you can do:
    Code:
     fseek(fp, 0L, SEEK_END);
     File_Size = ftell(fp);
    
     /*returns to the beginning */
     fseek(fp, 0L, SEEK_SET);
    Then return "File_Size / sizeof(COLOR)"

    good luck
    Last edited by Dharius; 03-14-2002 at 03:56 PM.
    Dharius

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Read the file and test your array limit, if you get within say 5 elements then call realloc and resize the array by a large block. This will save you many repeated calls to realloc, you'll gain in efficiency as well as sanity.

    >*ppcolor = realloc (*ppcolor, (size + 1) * sizeof (COLOR));
    Um, this is very unsafe, what happens if realloc fails and returns NULL? Well, not only do you have an error condition with your allocation, but you've also lost everything you had before. Create a temp pointer that will hold the result of realloc, test it for NULL and if it succeeds then deal with the data and assign the newly allocated memory to your ppcolor pointer. If it fails you can handle the error without losing everything.

    -Prelude
    My best code is written with the delete key.

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Dharius
    I can not remember if there is a function to directly get its size,
    There was some discussion on that in this thread.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 04:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 03:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 01:10 PM

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