Define an array, then resize it

I come from a java background and there is something I can do in Java, what I need to do in C ++, but I'm not sure how to do it.

I need to declare an array, but at the moment I do not know the size. As soon as I know the size, I set the size of the array. I am java I would just do something like:

int [] array; 

then

 array = new int[someSize]; 

How to do it in C ++?

+4
source share
7 answers

In C ++ you can:

 int *array; // declare a pointer of type int. array = new int[someSize]; // dynamically allocate memory using new 

and as soon as you finish using memory .. de-allocate it using delete, like:

 delete[]array; 
+15
source

you want to use std::vector in most cases.

 std::vector<int> array; array.resize(someSize); 

But if you insist on using new , then you will do a little more work than Java.

 int *array; array = new int[someSize]; // then, later when you're done with array delete [] array; 

There is no C ++ runtime when collecting garbage by default, therefore delete[] is required to avoid memory leak. You can get the best of both worlds using a smart pointer type, but really just use std::vector .

+25
source

The best way is to use std::vector . It does everything you need and is easy to use and learn. Also, since this is C ++, you should use vector instead of an array. Here is a great reason why you should use a container class (vector) instead of an array.

Vectors are dynamic in size and grow as needed - exactly what you want.

+1
source

The exact answer:

 char * array = new char[64]; // 64-byte array // New array delete[] array; array = new char[64]; 

std::vector is a much better choice in most cases. It does what you need without manual removal and new commands.

+1
source

As already mentioned, std::vector is usually the way to go. The reason is that the vector is very well understood, it is standardized for all compilers and platforms, and, above all, it protects the programmer from the difficulties of manual memory management. Moreover, vector elements must be selected sequentially (that is, vector elements A, B, C will be displayed in continuous memory in the same order in which they were entered into the vector). This should make the vector as cache-friendly as a regular dynamically distributed array.

While the same end result can be determined by declaring a pointer to int and manual memory management, this would mean additional work:

  • Each time you need more memory, you must manually allocate it
  • You must be very careful to delete previously allocated memory before assigning a new value to the pointer so that you are not stuck in huge memory leaks.
  • Unlike std::vector , this approach is not RAII - friendly. Consider the following example:

     void function() { int* array = new int[32]; char* somethingElse = new char[10]; // Do something useful.... No returns here, just one code path. delete[] array; delete[] somethingElse; } 

It looks healthy and safe. But this is not so. What if, when I try to allocate 10 bytes for "somethingElse", the system runs out of memory? An exception of the std::bad_alloc type will be std::bad_alloc , which will begin to spin the stack looking for the exception handler, skipping the delete statements at the end of the function. You have a memory leak. This is just one of many reasons to avoid manually managing memory in C ++. To fix this (if you really want it), the Boost library provides a bunch of good RAII wrappers like scoped_array and scoped_ptr .

+1
source

use std :: array when size is known at compile time, otherwise use std :: vector

 #include <array> constexpr int someSize = 10; std::array<int, someSize> array; 

or

 #include <vector> std::vector<int> array; //size = 0 array.resize(someSize); //size = someSize 
0
source

Declare a pointer:

 int * array; 
-2
source

All Articles