establishing new size using realloc

This is a discussion on establishing new size using realloc within the C++ Programming forums, part of the General Programming Boards category; What new size should I assign my array when using realloc if i have an array of floats and want ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Question establishing new size using realloc

    What new size should I assign my array when using realloc if i have an array of floats and want to increase it by one cell.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need to know how big your array is:
    Code:
    int main() 
    { 
      float *array = NULL;
      int i = 0;
    
      for(i = 0; i < 10; i++)
      {
        array = (float *)realloc(array, sizeof(*array) * (i+1));
        array[i] = 1;
      }
    
      free(array);
      return 0; 
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,902
    Mmm, now shall I point out the subtle bug in this code or not...

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Mmm, now shall I point out the subtle bug in this code or not...

    I'm guessing, how about memory leak?

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Salem
    Mmm, now shall I point out the subtle bug in this code or not...
    Don't know what's wrong with it so.... yes please.

  6. #6
    Sayeh
    Guest
    You are not using 'sizeof' on a variable _type_, and more seriously, you are scribbling into RAM because your 'array' wasn't ever allocated. You only created space for the pointer that would contain the address of the memory allocated to the array.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Sayeh
    You are not using 'sizeof' on a variable _type_, and more seriously, you are scribbling into RAM because your 'array' wasn't ever allocated. You only created space for the pointer that would contain the address of the memory allocated to the array.
    When I change the array from float to double the sizeof(*array) returns 8 bytes.
    So, still don't know what's wrong with it.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You are not using 'sizeof' on a variable _type_
    Are you sure about that? By taking the size of a dereferenced variable, you get the size of it's type. Not only is this correct, it's recommended because you don't bind the function call too closely with the type. I'm not quite sure about C++, but this is considered a safer and more maintenance friendly method for allocating memory in C.

    >Don't know what's wrong with it so.... yes please.
    Code:
    int main() 
    { 
      float *array = NULL;
      int i = 0;
    
      for(i = 0; i < 10; i++)
      {
        array = (float *)realloc(array, sizeof(*array) * (i+1));
        /* Bang! Your foot is history if realloc fails */
        array[i] = 1;
        /* To be correct, this should be array[i] = 1.0f; */
      }
    
      free(array);
      return 0; 
    }
    My best code is written with the delete key.

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Prelude, you're right about the realloc returning a NULL pointer.
    Not exactly a bug but still bad progamming. I think I should change my signature....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  2. Size of a string
    By edunia11 in forum C Programming
    Replies: 22
    Last Post: 04-11-2007, 05:13 AM
  3. char problem
    By eXistenZ in forum Windows Programming
    Replies: 36
    Last Post: 02-21-2005, 06:32 AM
  4. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM
  5. realloc()
    By PutoAmo in forum C Programming
    Replies: 3
    Last Post: 03-15-2002, 06:18 AM

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