How do I declare MULTIPLE 2d arrays in C++ using new? - c++

There is a similar topic about this.
How do I declare a 2d array in C++ using new?
What I want to do is to create multiple 2d arrays according to some integer which determines how many 2d arrays there should be.
I want to create a single dimensional array first for pointers and assing every pointer to a multidimensional array, using new. But it seems like you can't ask for memory to create multidimensional array. Why can't we just write:
int** howManyPointers = new int*[translate];
for (int i = 0; i < translate; i++){
howManyPointers[i] = new char[rowsUsed][2000];
}
In my project, 2d array must have 2000 columns but row size is undetermined first. It will be given by the user. Assume you've already got it [rowsUsed]
So what?

You allocate array of pointers, and then for each pointer you allocate 1d array like this:
int** 2dArray = new int*[rows];
for (int i = 0; i < rows; ++i) {
2dArray[i] = new int[cols];
}
and then you can do 2dArray[X][Y] for each X < rows and Y < cols;

Related

new bool** matrix vs new bool*matrix vs new bool matrix different types of cpp initialization confusion?

So,far after doing my own research i know bool** matrix will create 2d array,bool* matrix will create 1d array. Now,when implementing graphs using adjacency matrix
private:
bool** adjMatrix;
int numVertices;
public:
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];//<---here
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];//<--here
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
}
}
Now, i commented the part where i am confused, see both lines look similar to me as per my above mentioned understanding.
new bool*[numVertices] allocates and constructs an array of bool* (pointer to bool) of length numVertices. new bool[numVertices] allocates and constructs an array of bool of length numVertices.
What your code is doing is:
Declaring adjMatrix as a pointer to a pointer to bool
Setting adjMatrix to point to the first element of an array of bool*
Setting each element of that array to point to the first element of an array of bool
This gives the effect of adjMatrix looking like a 2D array, since you can write adjMatrix[i][j] and access one of the bool elements. However, this setup is not ideal for a few reasons.
It takes two pointer derefs to reach an array element rather than one
You have to do numVertices+1 dynamic allocations
The calls to new bool[numVertices] are not guaranteed to put all of the bools next to each other in memory
It would be better to make adjMatrix just be a bool*, and allocate all the bools at once:
adjMatrix = new bool[numVertices * numVertices];
This will save you numVertices dynamic allocations and put all the bools in a contiguous block of memory (better for cache). You could then access the array elements like
*(adjMatrix + i*numVertices + j)
instead of
adjMatrix[i][j]

Different ways of dynamically allocate 2D array with variable lengths

I searched a lot how to allocate dynamic 2D array of variable lengths(row and col)
I found these two ways:
1st one:
int (*array) [M] = new int[N][M];
2nd one:
int ** array = new int[row];
for(int i =0; i<width;i++){
array[i] = new array[col];
}
I got confused and don't know what is the difference and which is better.

dynamic memory allocation of 2d array in which columns are of different size

i want to create a 2d array dynamically in c++ language. But in that 2d array columns should be of different size. i mean to say that 2d array should not be in M * N.
It should be something like....
1 2 next line
3 4 5 next line
2 3 4 5 next line
5 next line
4 5 7
I am able to create 2d array in above manner but how to display content of array continously create a problem for me.
Please anyone explain me how to come up with this problem.
The best way to do this will be by using vectors. They are resizable arrays which handle all the memory management automatically. In this case, you can create a 2-D vector.
However, if for some reason you do not want to use vectors and want to use C-style arrays, then you can do it by creating an array of pointers and allocating different amounts of memory to each one of them. For storing their size, we can follow the strategy of allocating an additional cell in every array which will store the size of that array.
int main()
{
const int no_of_arrays=10;
int array_size[no_of_arrays]= {1,4,2,4,3,6,8,9,1,3}; // Different size for each array
int *p[no_of_arrays]; // An array of pointers
for(int i=0; i<no_of_arrays; i++)
{
p[i]=new int[array_size[i]+1]; // Allocating
p[i][0]=array_size[i]; // The first cell holds the size of the array
for(int j=1; j<=p[i][0]; j++)
{
p[i][j]=10; // Fill the array with the desired values;
}
}
// Display the arrays
for(int i=0; i<no_of_arrays; i++)
{
for(int j=1; j<=p[i][0]; j++)
{
std::cout<<p[i][j]<<" ";
}
std::cout<<"\n";
}
/*
*
* Do your thing with the arrays.
*
*/
// Deallocate the memory allocated to the arrays
for(int i=0; i<no_of_arrays; i++)
{
delete[] p[i];
}
}
However, doing this is not recommended as it can cause lot of problems ( For example memory leaks in case you forget to use delete after new ). Prefer to use vectors instead in case you don't know the size of the array beforehand.

Access an element of a 2d dynamic array--c++

I created a dynamic array.
typedef float* DynamicMatrix[MAT_SIZE];
DynamicMatrix matDyn;
// allocate rows and initialize to 0
for (r = 0; r < MAT_SIZE; r++) {
matDyn[r] = new float[MAT_SIZE];
for (c = 0; c < MAT_SIZE; c++) {
(matDyn[r])[c] = 0; // IS THIS CORRECT???
}
}
The whole idea was that I create an array of pointers which is my spine of a matrix I am creating. This spine is the leftmost-vertical part of the matrix. Each row of this array will point to another array of floats, therefore making it a dynamic 2d array.
I am unsure how to access an element of this matrix. Please let me know how to.
You can access it just like a "normal" matrix:
matDyn[r][c].
You can just access it by stating matDyn[r][c]
My error was that later in my program(not shown), I was trying to set matDyn[MAT_SIZE][MAT_SIZE] which isn't possible (it can only be matDyn[MAT_SIZE-1][MAT_SIZE-1]) and that is why I was getting my segmentation error.

C++ - dynamic array in 1D works, the same in 2D doesn't work [duplicate]

This question already has answers here:
How to create 2d array c++?
(5 answers)
Closed 9 years ago.
I have a problem with my code. I have some input for the class, the nmax und mmax. These are defined in header as
int nmax;
int mmax;
Then I have some arrays, defined in header as
double* Nline;
double** NMline;
and then I would like to allocate them in the main program. First, I assign to nmax und max a value from the input
nmax = nmax_in;
mmax = mmax_in;
and then I allocate the arrays
Nline = new double [nmax];
NMline = new double [nmax][mmax];
The problem is, the 1D array is this way allocated. But the 2D array not - the compiler writes:
expression must have a constant value
Why the NLine was allocated and NMline not?
I understand but I don't know how to do it in my program and why for the 1D array this allocation is OK.
Many thanks for your help
double** NMline;
will declare pointer to array of pointers, it will not declare 2D array. You need to first allocate data for the array of pointers (pointers to rows):
NMline = new double*[nmax];
and then to initialize each row:
for(int i = 0; i < nmax; i++)
NMline[i] = new double[mmax];
Don't forget to first delete all rows, and then the NMline itself:
for(int i = 0; i < nmax; i++)
delete [] NMline[i];
delete [] NMline;