Equiv of Realloc in C++?

This is a discussion on Equiv of Realloc in C++? within the C++ Programming forums, part of the General Programming Boards category; As I'm sitting here going through some old posts on the differences between the new/delete vs. malloc/free approaches to allocating ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    5

    Equiv of Realloc in C++?

    As I'm sitting here going through some old posts on the differences between the new/delete vs. malloc/free approaches to allocating memory I came up with a question.

    What is the new/delete approach to realloc? How is this accomplished?

    Let's say I create an int array ...

    Code:
    int *x = new int [5];
    
    /*And later in my program I *may* need to reallocate 
    that int to 10 elements.  How can this be done with the 
    new/delete approach?*/

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    There's no direct equivalent. You'll either have to allocate a new array of the extended size, copy the old contents across and then delete the old array, or use std::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, 08:26 AM
  2. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  3. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  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 realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 09:18 PM

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