I forgot how to initialize the array of pointers in C++ like the following:
int * array[10];
Is this a proper solution like this? Here:
array = new int[10];
// Is this the correct way?
int * array[10];
defines 10 pointers on 10 int arrays statically
To go dynamic:
int **array = new int *[10];
Better solution since you use C++: use std::vector
std::vector<int *> v;
v.resize(10);
v[2] = new int[50]; // allocate one array
Since we're using vectors for the array of pointers, lets get rid of the pointers completelely
std::vector<std::vector<int> > v;
v.resize(10);
v[2].resize(50); // allocate one array
Then access the array like a matrix:
v[3][40] = 14;
Going further, one way to initialize all the rows, using C++11, making a 10x50 int matrix in the end (but size can also change within the loop if we want). Needs gcc 4.9 and g++ -std=c++11 to build
std::vector<std::vector<int> > v;
v.resize(10);
for (auto &it : v)
{
it.resize(50); // allocate arrays of 50 ints
}
std::array for the static part, e.g. std::array<std::vector<int>, 10>.In general in most cases there is no great sense to initialize the array with exact addresses. You could assign the addresses or allocate appropriate memory during the usage of the array.
Usually there is sense to initialize an array of pointers with null pointers. For example
int * array[10] = {};
If you want to declare the array and at once to allocate memory for each element of the array you could write for example
int * array[10] =
{
new int, new int, new int, new int, new int, new int, new int, new int, new int, new int
};
or
int * array[10] =
{
new int( 0 ), new int( 1 ), new int( 2 ), new int( 3 ), new int( 4 ), new int( 5 ), new int( 6 ), new int( 7 ), new int( 8 ), new int( 9 )
};
But in any case it would be better to do the assignment using some loop or standard algorithm because in general the array can have more than 10 elements.
Also you should not forget to delete all allocated memory. For example
std::for_each( std::begin( array ), std::end(array ), std::default_delete<int>() );
Or if you have already defined objects of type int you could write for example
int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
//...
int * array[10] =
{
&x0, &x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9
};
Such an initialization is used very often for arrays of function pointers.
int **array = new int*[length];
Or, without dynamic memory allocaction :
int *array[10];
First int* array[10] would create an array of 10 Int pointers, which would be initlized to garbage values so best practice for that is
int* array[10];
for(int i = 0;i<10;i++)
{
array[i] = NULL;
}
That will safely unitize all pointers to an appropriate value so that if you try and access one that you haven't stored something in it will throw a seg fault every time.
Second array = new int[10] is creating a pointer to a array of 10 integers which is a completely different thing. If you want to set up an array of pointers using new that would be
int** array = new int*[10];
int * array[10] = {}; Done. No need for loops.You can have something like this -
int **array = new int*[n](); //n is the number of pointers you need.
//() after declaration initializes all pointers with nullptr
int **array = new int*[n](); This initializes the array as well.You can use
int* a = {new int(....), new int(....) ,...
Or something like below
for (int b = 0; b < 10; ++b)
a[b] = new int(....);
I think this link has a good explanation about array of pointers.
int * array[10] = {};That initializes the pointers tonullptr.int * array[10] = {new int, new int, ...};?