Thread: Quick realloc question

  1. #1
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45

    Quick realloc question

    When using realloc to resize an array of say six ints to one of five ints, is it the sixth int "array[5]" that gets junked or is there no protocol?

    Matt

  2. #2
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    K&R book says if object is smaller, it gets truncated. If larger, the extra space is uninitialized. If realloc returns NULL, then nothing happened (fail). So basically, it handles the size change, leaving the rest up to you. Hope that helps.

    -Joe

  3. #3
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45
    Thanks. When I came to code the problem that had provoked the question, I actually found it wasn't an issue.

    None of the online man pages say what happens when you realloc down to a smaller sized piece of memory. I didn't think it was that trivial...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I hope you're not doing
    p = realloc( p, newsize );
    even if you are shrinking the memory.
    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.

  5. #5
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45
    *coughs nervously* I might be...

    ... if I were, what should I do instead?

    (Also , if I used that old line "it seems to work" - what would be a sign that it actually wasn't?)

    Matt

  6. #6
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45

    Context

    Code:
    void update(int deleteme, int array[], int size)
    {
      int i,j=0;
      int *temp;
      temp=array;
      while(array[j]!=deleteme)
        j++;
      array=realloc(array,sizeof(int)*(size-1));
      for(i=0;i<size;i++){
        if(i<j)
          array[i]=temp[i];
        else
          array[i]=temp[i+1];
      }
      free(temp);
      return;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yep, that's pretty bad
    > temp=array;
    This is not a copy of the whole array, merely just a copy of the pointer to it. Which means you're likely to either free something which isn't even allocated, or free your real array.
    Both are bad news.

    I would go with something like this (untested)
    Code:
    for ( i = 0 ; i < size ; i++ ) {
      if ( array[i] == deleteme ) break;
    }
    if ( i != size ) {
      void *temp;
      // move everything else down one index
      memmove( &array[i], &array[i+1], (size-i-1)*sizeof(int) );
      // resize the array
      temp = realloc( array, sizeof(int)*(size-1));
      if ( temp != NULL ) {
        array = temp;
      } else {
        // do something else with array
      }
    }
    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.

  8. #8
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45
    The thing is, I want to delete only "deleteme" and keep the rest of the array intact, but less that one element. It seems like such a simple thing to do... yet it's driving me nuts!

    I've tried to kid myself that what I had worked but all the problems with the rest of my code seem to stem from this bug...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM