1

I have an array of characters allocated with new and i want to modify the size of the array. Can i use realloc function for that? What is the best way to do so?

1
  • 2
    Use vector if you need to resize a lot. Commented Dec 11, 2013 at 12:16

5 Answers 5

5

No, you can't... realloc() can only be used with malloc()/free()

Best call for a new[] allocated array is to create a new one and then memcpy() the data from one to another.

Better way - use an std::vector or std::string instead of array if you know you'll need resizing. Internally they're pretty much the same array.

Sign up to request clarification or add additional context in comments.

6 Comments

I'd mention deleting the first array.
I assume this is obvious.
@DarkWanderer:This means i need to allocate a new array, copy the data from old array to the new array and then delete the old array?
@user3090837: yes... That's what realloc() does internally, anyway. I recommend memcpy() because it can be made intrinsic (i.e. hardware-optimized) for maximum speed.
@DarkWanderer And std::copy is a template, which means that the compiler has the full source available, and can generate the best possible code as well.
|
4

In C++ it is best to use the STL std::vector class for this kind of thing. Either that, or a std::string.

Comments

2

I have an array of characters allocated with new and i want to modify the size of the array.

You can't resize an array, you can only allocate a new, larger one, move the contents to the new array, and delete the old one.

Can i use realloc function for that?

If you used malloc to allocate the original array, yes. But that's usually a bad idea in C++, where you usually want to deal with arrays of non-trivial objects not raw memory.

What is the best way to do so?

Use std::string (or perhaps std::vector<char>) to manage a dynamic array of characters automatically. These also have the advantage of using RAII to reduce the risk of memory leaks and other memory management errors.

Comments

2

You can use realloc(), if your array is allocated dynamically(via malloc/calloc/realloc). If you have static array, you can't resize it.If you have allocated with new:

int* Copy = new int[newSize];
std::copy(oldCopy,oldCopy+size,Copy);

But the best way would be to use std::vector<type> from c++ standard library

2 Comments

It's undefined behaviour to use realloc on something that was not obtained by malloc/calloc/realloc.
OP specified it was allocated with new, not malloc.
0

As was said by others, you cannot resize the array that was allocated per se, but you can create a larger one, copy the content of the first array to the second and delete the first one.

Here's an example, using std::copy().

int main(int argc, char** argv) {
    int* arr = new int[5];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    arr[3] = 4;
    arr[4] = 5;
    int* tmp = new int[10];
    std::copy(arr, arr + 5, tmp);
    std::copy(arr, arr, tmp + 5);
    delete[] arr;
    arr = tmp;

    for(int i = 0; i < 10; i++) {
        std::cout << arr[i] << "\n";
    }
    delete[] arr;
    std::cin.get();
    return 0;
}

This first creates an integer array and fills it. It then creates a larger array and fills it with the content of the first array twice and display that new larger array.

The principle is the same for an array of characters.

As the others have mentionned, your best bet in C++ is to use the standard library. For a resizable array of characters, you should probably use std::string or a vector of strings, but it's a bit overkill in some situations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.