Berkeley CSUA MOTD:Entry 33716
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2018/10/21 [General] UID:1000 Activity:popular
10/21   

2004/9/23 [Computer/SW/Languages/C_Cplusplus] UID:33716 Activity:high
9/23    Is the a C++ equivelent to the C realloc function?
        \_ realloc(). But seriously, maybe you want an STL container
           that automatically grows.
           \_ Not in this case.  I just need to grow a char array
              automatically myself.   (For speed reasons) Really, It's a
              very isolated place in the code, and I'll probably just end
              up switching the news and deletes to mallocs and frees, and
              therefore use realloc when I need to grow the array.  But I
              was curious if there was an equivelent that would work with
              new and delete.
              \_ No, there is no equivalent for new/delete.  You probably
                 should avoid using arrays and new/delete directly anyway.
                 It's hard to make it exception-safe.  Either use an STL
                 container (e.g. vector or string; shrinking a vector
                 doesn't free memory to the system though) or write your
                 own container that uses malloc/free/realloc.
              \_ If you need to do this for performance then you should avoid
                 new/delete, and especially STL containers.  STL containers
                 require new elements to be default initialized, etc.  I wrote
                 my own "pod_vector" for POD types that uses malloc/realloc
                 etc. for high performance.  If you do this, it's important to
                 have unit tests.