0

I am new to C++ programming but I have experience in java.

In java I can create an array by allocating it a size only.

int[] array = new int[10] // 1

Later, I can assign a new array to the existing one.

array = new int[4]; // 2

Similarly, in C++, I can create an array using:

int array[10]; // 3

This will allocate an array of 10 elements. How can I achieve step 2 in C++ like I did in java.

Update:

Thanks for your help @idclev-463035818 but, the question you referenced me to does not answer my question. I want to assign a new array to an existing one with a different size. I don't want to copy the elements of the existing array. Also, I don't want to use pointers or dynamic array. I just wanted to know if step 2 is possible in C++ or not.

  • in c++ you would use a `std::array` instead of a c-array, they can be copied, see the duplicate (or `std::vector` for dynamically sized arrays) – 463035818_is_not_a_number Mar 25 '20 at 12:46
  • btw dont get mislead by java and c++ syntax having lots of similarities, they are very different languages – 463035818_is_not_a_number Mar 25 '20 at 12:47
  • In relation to what @idclev463035818 said, beware of using `new` in C++. It's not like the `new` in Java. – Sean Francis N. Ballais Mar 25 '20 at 12:48
  • I think the direct C++ equivalent of what you showed is making `array` a pointer to a heap-allocated array (one created using `new`). When you perform Step 2, you just create a new heap-allocated array. – Sean Francis N. Ballais Mar 25 '20 at 13:06
  • `int *array = new int[10]; delete[] array; array = new int[4];` Or, use a `vector`. Step 2 is not possible without dynamic arrays. Java does not have an equivalent to `int array[10]`, so step 2 is exactly the same in C++ and Java, both using dynamically allocated arrays. – ChrisMM Mar 25 '20 at 14:05

2 Answers2

1

I want to assign a new array to an existing one with a different size. I don't want to copy the elements of the existing array. Also, I don't want to use pointers or dynamic array.

You have do one of those two things.

I just wanted to know if step 2 is possible in C++ or not.

No.

Asteroids With Wings
  • 16,602
  • 2
  • 20
  • 35
  • 1
    You should probably rephrase. Step 2 is possible in C++, using dynamic arrays (which is what Java is doing anyway) … It's not possible with statically sized arrays though. – ChrisMM Mar 25 '20 at 18:02
  • @ChrisMM Step two is assigning an array to an existing one. Never possible. All you can do there is create a new one and re-use some old pointer to get access to it. A perhaps subtle but certainly important distinction! C++ simply has no dimension-mutation facility for array types. Java's array types are rather different (at point of use). Yes, sure, you can simulate that, but to do that you have to fall back on the alternatives that the OP specifically ruled out. – Asteroids With Wings Mar 25 '20 at 18:24
  • Step 2 is simply `array = new int[4];`, which can easily be done if `array` is `int *array`, which is more or less equivalent to Java's `int[] array` – ChrisMM Mar 25 '20 at 18:51
  • @ChrisMM But, again, that is _not_ the same thing, and simply assigning is _broken_ (e.g. you haven't freed the old pointee). – Asteroids With Wings Mar 25 '20 at 21:45
1

I would Suggest using a Std::Vector. They are basically dynamically allocated Array's and have built in functions that relate to copying a vector to another. you can read more about vectors here : http://www.cplusplus.com/reference/vector/vector/

what you want to do directly is not possible in C++. you would either have to copy the array or use pointers in a loop.