473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
Jul 22 '05 #1
9 7730

"James" <me******@mail1.monash.edu.au> wrote in message
news:dc**************************@posting.google.c om...
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[i] = 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
Jul 22 '05 #2
James wrote:
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.
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"James" <me******@mail1.monash.edu.au> wrote in message
news:dc**************************@posting.google.c om...
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[i] = 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[i] = 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.
Jul 22 '05 #4

"James" <me******@mail1.monash.edu.au> schrieb im Newsbeitrag
news:dc**************************@posting.google.c om...
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
Jul 22 '05 #5
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
Jul 22 '05 #6
In message <cm**********@nntp1.jpl.nasa.gov>, E. Robert Tisdale
<E.**************@jpl.nasa.gov> trolled
James wrote:
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.

--
Richard Herring
Jul 22 '05 #7


In message <cm**********@nntp1.jpl.nasa.gov>, E. Robert Tisdale
<E.**************@jpl.nasa.gov> trolled
James wrote:
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[i] = new double [m];
//Here you can use pp[i][j]
....

// Deallocation
for(register int i=n-1; i>=0; i--)delete [] pp[i];
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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #8
Dear All,

Many thanks for your help! I got it!

Regards,

James
Jul 22 '05 #9
Leslaw Bieniasz wrote:
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[i] = new double [m];

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

// Deallocation
for(register int i=n-1; i>=0; i--)delete [] pp[i];
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[i] = pp[i-1] + m;

// use pp[i][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/
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
4
by: seesaw | last post by:
class A { public: static A* newA() { return new A; } .... }; In the code, two things not very clear and natural to me: 1. the method newA() is defined as static. 2. newA as a member method...
18
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
1
by: sven_c_t | last post by:
Hi! Probably a newbie question. I´ve been working a bit with the widget toolkit FLTK and I have been wondering why there is such a heavy use of the new operator, when it does not seem to be...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
36
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple,...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.