a C++ equivilent to C's realloc?

This is a discussion on a C++ equivilent to C's realloc? within the C++ Programming forums, part of the General Programming Boards category; is there an equivilent to C's realloc? i think i may want to experiment with it and see if i ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    21

    Arrow a C++ equivilent to C's realloc?

    is there an equivilent to C's realloc? i think i may want to experiment with it and see if i can make a dynamically sized random access array, but i also think it might get fragmented, so arrays no longer work. anyways, is there?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    No...C++ doesnt give a conversion of realloc.....

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    21
    oh well, now that i think about it things could get realllly screwed up. if you allocated an array 'x' with 10 elements and then array 'y' took up the space afterwords, and then reallocated x for 20 elements, it might not be continuous so it can't be used like a continuous array. oh well, worth a shot i guess

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it might not be continuous so it can't be used like a continuous array.
    Actually, if the new size can't be allocated with the current block, realloc is free to allocate a new block that is large enough, but in a different location. This is why any existing pointers to data inside of that block can be invalidated after a call to realloc.

    In C++ you can fake realloc by allocating a new block with the new size and copying the elements to that new block, then delete the old block and reassign the pointer.

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

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    realloc, meet the vector.

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 realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 08:18 PM

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