how to "new" a two-dimension array in C++?

J

James

Hi,

I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Looking forward to hearing from you!

Regards,

James
 
E

E. Robert Tisdale

James said:
I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Forget about new and try this:

double pp[dynamic_size][dynamic_size];

Variable length arrays are included in the C99 standard
and will be included in the new C++ standard.
Your compiler probably supports them already.
 
J

John Harrison

James said:
Hi,

I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?

A 2D array is a one dimensional array whose elements are other one
dimensional arrays. So you can just use new.

int (*a)[20] = new int[x][20];

but this probably isn't what you want because the second dimension is fixed
(20 in this case). You can also create an array of pointers, and make those
pointers point to other arrays.

int **a = new int*[x];
for (int i = 0; i < x; ++i)
a = new int[y];

this gives you a 2D array which is variable in both dimensions. But its ugly
code, the sort of thing a C programmer would do.
Is the only way to create a class?

No, but obviously it is prefereable to either create a class or use one that
has already been created. C++ has a vector class which you can use to as a
2D 'array' (a vector of vectors). It also has a class called valarray which
can also be used to create 2D 'arrays'
In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Well I've given you four different options, take your pick, vector would be
my first choice.

john
 
M

Method Man

John Harrison said:
James said:
Hi,

I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?

A 2D array is a one dimensional array whose elements are other one
dimensional arrays. So you can just use new.

int (*a)[20] = new int[x][20];

but this probably isn't what you want because the second dimension is fixed
(20 in this case). You can also create an array of pointers, and make those
pointers point to other arrays.

int **a = new int*[x];
for (int i = 0; i < x; ++i)
a = new int[y];

this gives you a 2D array which is variable in both dimensions. But its ugly
code, the sort of thing a C programmer would do.


Here is another method which calls 'new' only twice and allows all elements
of a 2D int array to be contiguous in memory:

int** a = new int*[x];
int a[0] = new int[x * y];
for (int i = 1; i < x; i++)
a = a[0] + i * y;

If you don't need to extend the size of your array later (perhaps dealing
with matrices), then this method is very efficient.
 
G

Gernot Frisch

James said:
Hi,

I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by "double[][] pp
=
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Write a template class, that has a parameter for the dimensions. Then
use:

vector<vector<Type> > data;
and overload the [] operator to see, whether there is data at the
requested position / insert empty types or so. I'm a bit tired today,
but I hope you get the idea...
-Gernot
 
J

JKop

James posted:
Hi,

I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Looking forward to hearing from you!

Regards,

James

Unfortunately, the following is illegal:

#include <iostream>
#include <cstddef>

int main()
{
std::size_t random1 = 67;
std::size_t random2 = 54;

int (&multi)[random1] = *reinterpret_cast< int (* const)[random1] >
( new int[random1][random2] );
}


I think you'd be better off using something like "std::vector".


-JKop
 
R

Richard Herring

E. Robert Tisdale said:
James said:
I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?
In java, I can easily create a two-dimension array by "double[][] pp

new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Forget about new and try this:

double pp[dynamic_size][dynamic_size];

Variable length arrays are included in the C99 standard

You seem to have strayed into the wrong newsgroup. This is clc++
and will be included in the new C++ standard.

Says who?
Your compiler probably supports them already.

Name five.

When trolling, please don't pick on newcomers who just came here for
helpful advice.
 
L

Leslaw Bieniasz

E. Robert Tisdale said:
James said:
I am new to C++. I want to directly create a dynamic two-dimension
double array, i.e. double pp[][]. I found the "new" is only for
one-dimension array, i.e. double *p = new p[dynamic_size]. How to
"new" a two-dimension array?
Is the only way to create a class?
In java, I can easily create a two-dimension array by "double[][] pp

new double[dynamic_size][dynamic_size]". But, how to do it in C++?


What is wrong with the standard way?:

double **pp;
int n,m;

// Allocation
pp = new double* [n];
for(register int i=0; i<n; i++)
pp = new double [m];


//Here you can use pp[j]
....

// Deallocation
for(register int i=n-1; i>=0; i--)delete [] pp;
delete [] pp;


The only problem is that the above does not provide bounds checking.
Writing a class with index checking will be more safe, but substantially
slower in use, although you can speed up things a little by internally
implementing the matrix as a one-dimensional vector.

L.B.


*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
E

E. Robert Tisdale

Leslaw said:
What is wrong with the standard way?:

double **pp;
int n,m;

// Allocation
pp = new double* [n];
for(register int i=0; i<n; i++)
pp = new double [m];

//Here you can use pp[j]
...

// Deallocation
for(register int i=n-1; i>=0; i--)delete [] pp;
delete [] pp;


That's lame. Try

// allocation
double** pp = new double* [n];
pp[0] = new double [n*m];
for (int i = 1; i < n; ++i)
pp = pp[i-1] + m;

// use pp[j]

// deallocation
delete [] pp[0];
delete [] pp;
The only problem is that the above does not provide bounds checking.
Writing a class with index checking will be more safe, but substantially
slower in use, although you can speed up things a little by internally
implementing the matrix as a one-dimensional vector.

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/
 
Joined
Jun 4, 2011
Messages
6
Reaction score
0
full flach program to take a two dimensions run time array

////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

int main()
{
int cols, rows;

cout << "How many rows do you need in your matrix? ";

cin >> rows;

cout << "And ... How many columns? ";

cin >> cols;

//...............memory allocation..................//

int **myMatrix;

myMatrix = new int*[rows];

for (int i=0; i<rows; i++)

myMatrix = new int[cols];

//..................................................//

//............................Input..............................//

cout << "Now fill up your " << rows << "x" << cols << " matrix:\n";

for (int i=0; i< rows; i++)

for (int j=0; j<cols; j++)

cin >> myMatrix[j];

//...............................................................//

//............................Output.............................//

cout << "\nYou entered the following matrix\n";

for (int i=0; i<rows; i++)

{

for (int j=0; j<cols; j++)

cout << myMatrix[j] << " ";

cout << endl;

}

cout<<"(e-mail address removed)...";

//.................memory deallocation..............//

for (int i=0; i<rows; i++)

delete [] myMatrix;

delete [] myMatrix;

//..................................................//


system("pause");

return 0;
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top