38

I need to do the equivalent of the following C# code in C++

Array.Resize(ref A, A.Length - 1);

How to achieve this in C++?

2
  • 7
    Is there any reason why you don't use std::vector? Commented Sep 20, 2010 at 8:17
  • NB: You can't resize an array in C# either. They are fixed-size. Array.Resize is actually allocating a new array and reassigning the variable - hence the need for ref. The original array does not change. Commented Oct 18, 2022 at 18:04

5 Answers 5

64

You cannot resize array, you can only allocate new one (with a bigger size) and copy old array's contents. If you don't want to use std::vector (for some reason) here is the code to it:

int size = 10;
int* arr = new int[size];

void resize() {
    size_t newSize = size * 2;
    int* newArr = new int[newSize];

    memcpy( newArr, arr, size * sizeof(int) );

    size = newSize;
    delete [] arr;
    arr = newArr;
}

code is from here http://www.cplusplus.com/forum/general/11111/.

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

9 Comments

And what happens when between the new and the delete, an exception is thrown? -1 for suggesting manual resource management to a newbie. (If you want to make this safe, you will need a smart pointer. If you add a few more utility functions, you'll arrive at std::vector.)
Thanks for -1 :) I'm suggesting newbie to understand that no magic is happening during array resize, new array is created and all elements are copied and if you write a code which resizes array by 1 each time you need an extra element to add, then you will be wasting resources.
If exception will be thrown between new and delete this means you're in real trouble, and you cannot really do anything in the resize function, you shouldn't catch exception there (maybe just to rethrow some more informative one) and let the caller to handle it.
You should try do delete anyhow. That's what smart pointers are for.
@user: He said in between, not on. If anyone is ever in a position to manually free some resource, they've failed. It needs to wrapped, period. Not that hard.
|
49

The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector is for:

std::vector<int> v; // size of the vector starts at 0

v.push_back(10); // v now has 1 element
v.push_back(20); // v now has 2 elements
v.push_back(30); // v now has 3 elements

v.pop_back(); // removes the 30 and resizes v to 2

v.resize(v.size() - 1); // resizes v to 1

4 Comments

BTW, isn't array in C# is also static?
@ironic: I suppose so, otherwise the ref in iJeeves' code would not have been necessary.
SIDE NOTE: std::vector will use memory on heap. On the other hard std::array use memory always on stack.
@raz: Of course it will use heap memory, as there is no other way to change the size. (That's why std::array won't help here either: Yes, it's on the stack, but it's not resizable.)
4
  1. Use std::vector or
  2. Write your own method. Allocate chunk of memory using new. with that memory you can expand till the limit of memory chunk.

Comments

1

Raw arrays aren't resizable in C++.

You should be using something like a Vector class which does allow resizing..

std::vector allows you to resize it as well as allowing dynamic resizing when you add elements (often making the manual resizing unnecessary for adding).

Comments

1

You cannot do that, see this question's answers. You may use std:vector instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.