Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you resize a C++ array after initialization? [duplicate]

I'm learning to program, and C++ is my first language. Don't bother using pointers to show me - I don't understand them yet, and won't bother until I have more free time to dedicate to this.

    int mergeSort()
{
    const int n = 9;
    int originalarray[n] = {1, 3, 5, 7, 9, 2, 4, 6, 8};


    const int halfelements = (sizeof(originalarray) / sizeof(int)) / 2;
    int farray[halfelements];
    int sarray[halfelements];

    for (int i = 0; i < halfelements; i++) {
        farray[i] = originalarray[i];
    }

    for (int i = halfelements, x = 0; i < (halfelements * 2); i++, x++) {
        sarray[x] = originalarray[i];
    }

I was assigned (I'm not taking classes - just learning with a few friends helping me out) a merge sort algorithm, with the algorithm explained but not the implementation. I want to rewrite this so it will work for both odd and even integers. I tried adding this code:

if ((n % 2) != 0) int farray[halfelements + 1];

So that I could use the same integer to iterate over both subsequent arrays. A sizeof(farray) is showing to be 16 bytes, or 4 integers. So it isn't resizing. What I want to know - is it possible to resize arrays after they initialized?

Edit: How would I implement a vector? I don't understand how to use iterators in a loop to iterate over and copy the values.

like image 440
jkeys Avatar asked Apr 16 '09 16:04

jkeys


People also ask

Can you change size of array once created in C?

Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.

Is it possible to resize an array?

You cannot resize an array in C#, but using Array. Resize you can replace the array with a new array of different size.

Can we change the size of array at run time?

Size of an array If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

What would happen if you initialize the array with more values than the size of the array?

An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.


4 Answers

C++ arrays are fixed in size.

If you need a "resizable array", you'll want to use std::vector instead of an array.

like image 61
Legion Avatar answered Oct 13 '22 15:10

Legion


My advice is even stronger: use std::vector<> (et. al.) unless you have a very good reason to use a C-style array. Since you're learning C++, I doubt you have such a reason: use std::vector<>.

like image 33
Ðаn Avatar answered Oct 13 '22 15:10

Ðаn


I would also recommend std::vector. However if you are stuck with an array you can always malloc the memory and then realloc if you need to make the array larger.

Do a search here on SO, there is information about malloc and realloc.

like image 38
Gregor Brandt Avatar answered Oct 13 '22 14:10

Gregor Brandt


If you want to resize an array, you probably want to use a vector, which can be resized automatically.

like image 35
tstenner Avatar answered Oct 13 '22 16:10

tstenner