tags:

views:

177

answers:

6

Possible Duplicate:
Can you resize a C++ array after initialization?

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++?

+1  A: 

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).

paxdiablo
+13  A: 

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;

v.push_back(1);
v.push_back(2);
v.push_back(3);

v.resize(v.size()-1);
sbi
I would replace `v.resize(v.size() - 1)` with `v.pop_back()`...
FredOverflow
BTW, isn't array in C# is also static?
ironic
@FredOverflow: Yes, that's a possibility. However, I tried to stick to the code given in the question. I suppose that `size-1` was only a placeholder.
sbi
@ironic: I suppose so, otherwise the `ref` in iJeeves' code would not have been necessary.
FredOverflow
A: 

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

rics
+3  A: 

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/.

Kirill Muzykov
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`.)
sbi
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.
Kirill Muzykov
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.
Kirill Muzykov
You should try do *delete* anyhow. That's what smart pointers are for.
Matteo Italia
@Sbi, your comment is not entirely true. What if I overload new and delete such that they don't throw? What if my new and delete and actually allocating from pool and pool is handling the exception?
Manoj R
@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.
GMan
I agree, my fault. Memory should be freed and exception rethrown.
Kirill Muzykov
+2  A: 
  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.
alam
+1  A: 

Try realloc()

int size,*a;
a = 0;
size = 3;
a = (int*) realloc(a,size*sizeof(int));
a[0] = 16;
a[1] = 24;
a[2] = -5;
size ++;
a = (int*) realloc(a,size*sizeof(int)); //Reallocates more memory to the 'array'
a[3] = 44;
free(a); //Free the memory you allocated once you are done using it.

To call realloc() remember to #include <cstdlib>

Andrew Dunn
How will you copy content? Assuming at run time you require more memory
alam
`realloc()` does that automatically Sources: 1: (http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/) 2: (http://en.wikipedia.org/wiki/Malloc#realloc)
Andrew Dunn
Note that this only works for PODs. You shouldn't use `malloc()` and friends in C++.
sbi