2 dimensional arrays

C

Chris

Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

thnx
Chris
 
D

David Hilsee

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

This is covered in the FAQ (albeit in a question that seems unrelated). See
the FAQ (http://www.parashift.com/c++-faq-lite/), section 16 ("Freestore
management"), question 19 ("Does C++ have arrays whose length can be
specified at run-time?").
 
A

Ali Cehreli

is it possible to create a two dimensional array on the heap use a
similar syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

typedef is your friend:

#include <stddef.h>

size_t const row_count = 3;
size_t const col_count = 4;

typedef int Row[col_count];

int main()
{
Row * matrix = new Row[row_count];

for (size_t row = 0; row != row_count; ++row)
{
for (size_t col = 0; col != col_count; ++col)
{
matrix[row][col] = (row * 100) + col;
}
}

delete[] matrix;
}

Ali
 
J

Jason

I am no expert on C++ but I think you can try this:

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char **argc)
{
//Assuming you want a 2D int array like: myIntArray[100][20]
vector<int> temp(20, 0);
vector< vector<int> > myIntArray(100, temp);
return 0;
}

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?
 
F

Frédéric Lachasse

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

doing arr2[j] is in fact doing 2 function call: first arr2, then opn
the result calling operator[](j). To mimick the behaviour, your need to
define on arr an operator[](int) member that returns a type that defines an
operator[] that will return the right. For example, an int* to the first
element of the row:

class Arr2
{
int nbCols_; // nb columns per row
int* arr_; // pointer to allocated 2D array
public:
Arr2(int nbRows, nbCols) : nbCols_(nbCols), arr_(new int[nbCols *
nbRows]) {}
int* operator[](int row) { return arr_ + (row * nbCols_); }
// TODO: add necessary copy/assignment/destructor members
};


Arr2 arr2(2, 3);
arr2[0][2] = 7;

arr2[j] in fact does:
(arr_ + (i * nbCols_))[j]
which is the same as:
*((arr_ + (i * nbCols_)) + j)
or:
*(arr_ + (i * nbCols_ + j))
which yields the same result as:
arr_[i * nbCols_ + j]
 
P

Peter Ammon

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

int (*arr)[3] = new int[2][3];
arr[1][2] = 0;

-Peter
int (*arr)
 
D

Daniel T.

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

Don't do it! Use a class instead... Here is something to get you started:

template < typename T, typename U = std::deque< T > >
class Matrix {
public:
typedef typename U::size_type size_type;
typedef typename U::reference reference;
typedef typename U::const_reference const_reference;
typedef typename U::iterator iterator;
typedef typename U::const_iterator const_iterator;

Matrix(): h_limit( 0 ), v_limit( 0 ) { }
Matrix( size_type h, size_type v ):
buffer( h * v ), h_limit( h ), v_limit( v ) { }

reference operator()( size_type h, size_type v ) {
check_limits( h, v );
return buffer[h_limit * h + v];
}

const_reference operator()( size_type h, size_type v ) const {
check_limits( h, v );
return buffer[h_limit * h + v];
}

iterator begin() { return buffer.begin(); }
iterator end() { return buffer.end(); }
const_iterator begin() const { return buffer.begin(); }
const_iterator end() const { return buffer.end(); }


private:
void check_limits( size_type h, size_type v ) const {
if ( h >= h_limit || v >= v_limit )
throw std::eek:ut_of_range( "Matrix" );
}
U buffer;
size_type h_limit, v_limit;
};

The above was built with the help of the following:

int main() {
Matrix< double > m;
Matrix< char > m1( 2, 2 );
assert( m1( 0, 0 ) == 0 );
m1( 0, 0 ) = 1;
assert( m1( 0, 0 ) == 1 );
assert( m1( 0, 1 ) == 0 );
m1( 0, 1 ) = 2;
assert( m1( 0, 1 ) == 2 );
assert( m1( 1, 0 ) == 0 );
m1( 1, 0 ) = 3;
assert( m1( 1, 0 ) == 3 );
try {
m1( 2, 0 ) == 3;
assert( false && "out of range not caught h" );
}
catch ( out_of_range& err ) { }
catch ( exception& err ) {
cout << typeid( err ).name() << endl;
assert( false && "wrong error thrown h" );
}
try {
m1( 0, 2 ) == 3;
assert( false && "out of range not caught v" );
}
catch ( out_of_range& err ) { }
Matrix< char >::iterator begin = m1.begin();
assert( *begin++ == 1 );
assert( *begin++ == 2 );
assert( *begin++ == 3 );
assert( *begin++ == 0 );
assert( begin == m1.end() );
cout << "OK";
}
 
P

Paul

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

looks like fundamentals are missing here, there is no memory
allocation like two dimensional memory or three dimensional, it is how
we see and use it.
so int arr[2][3] and arr[6] are same in memory.
but to access (1,2) arr[1][2], looks as if we are acting on a
matrix(m,n) and arr[1*3 + 2] doesn't look like though, but it has its
own use.

or you wanted to do some thing like this??
int** ppArr = new int*[2];
ppArr[0] = new int[3];
ppArr[1] = new int[3];

-Paul.
 
G

Gorgen Givik

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?
Several. It is a good exercise in operator overloading, to try make it.
If you read the FAQ for this forum you get good answers. I took the
liberty to point you to the right page. The FAQ is actually a good
source of information.
http://www.parashift.com/c++-faq-lite/operator-overloading.html
 
K

kanze

is it possible to create a two dimensional array on the heap use a
similar syntax as with stack-vartiables :

Of course.
On the stack :
int arr2[2][3];
arr2[0][2] = 7;
On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

There's no problem if you get the types correct:

int (*parr2)[3] = new int [2][3] ;

Of course, in modern C++, we'd probably write:

std::vector< std::vector< int > >
a( 2, std::vector< int >( 3 ) ) ;

Although I'll admit that the syntax for a multiple dimension array isn't
one of std::vector's strong points.
 
U

Ulrich Eckhardt

Chris said:
is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

Use a class instead, boost one that features a two(ond more!) dimensional
array.
Oh, yes, you can allocate an array of pointers first and then allocate a
one-dimensional array in each slot there, but that is error-prone and
tedious.

Uli
 
P

puppet_sock

Chris said:
is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

In addition to what David said, consider whether you really
want arrays. Maybe you want to make use of one of the standard
container classes.
Socks
 
M

Matt

Chris you can do the following:

int **array;

array = new int*[size];
for( unsigned int i = 0; i < size; i++){
array = new int[size];}

This will allow you to access members as if the array was declared in
the stack.

array[2][3] = 8;

etc...

To clean up:
for( unsigned int i = 0; i < size; i++)
delete[] array;
delete[] array;

You can also have a look at the boost::multi_array library, which
works like a N dimensional vector. Hope this helps.

- Matt

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

thnx
Chris
 
I

Ioannis Vranos

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?


As few others said:


int(*parr2)[3]=new int[2][3];


or


typedef int whatever[3];


whatever *parr2=new int[2][3];



or

typedef int whatever[3];

whatever *parr2=new whatever[2];




Of course


vector<vector<int> > parr2(2, vector<int>(3));


makes more sense (meaning it is better to be used).






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
L

linestyle

Hi,
is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

thnx
Chris

may be you can use it:

typedef long * PLONG;
PLONG * pp=new PLONG[20];
pp[0]=new long[10];
pp[1]=new long[10];
pp[0][0]=1;
pp[0][1]=2;

delete []pp[0];
delete []pp[1];
delete []pp;
 
L

linestyle

Hi,
is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

thnx
Chris

may be you can use it:

typedef long * PLONG;
PLONG * pp=new PLONG[20];
pp[0]=new long[10];
pp[1]=new long[10];
pp[0][0]=1;
pp[0][1]=2;

delete []pp[0];
delete []pp[1];
delete []pp;
 
I

Ioannis Vranos

Chris said:
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?


As few others said:


int(*parr2)[3]=new int[2][3];


or


typedef int whatever[3];


whatever *parr2=new int[2][3];



or

typedef int whatever[3];

whatever *parr2=new whatever[2];




Of course


vector<vector<int> > parr2(2, vector<int>(3));


makes more sense (meaning it is better to be used).






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
R

Richard Kaiser

Try

int n=10,m=10; // must not be constant
vector< vector<int> > a(n,m);

You then can access the array elements like this

a[0][0]=1;

A vector is a wrapper for a dynamic array on the heap that is more easy
to use than one hand crafted with new.

Richard Kaiser

Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?
 
J

Jarek Krecik

Try this:
int** arr2 = new int*[2];
for(int i=0; i<2;i++)
arr2 = new int[3];

And use it like this;
for(int j=0;j<2;j++)
for(int k=0;k<3;k++)
arr2[j][k] = j+k;
Hi,

is it possible to create a two dimensional array on the heap use a similar
syntax as with stack-vartiables :

On the stack :
int arr2[2][3];
arr2[0][2] = 7;

On the heap :
I tried : int *parr2 = new int [2][3];
but in vain --> compiler error

The only way i see is :
int *parr2 = new int [2 * 3];
but then
parr2[6] = 1; ==> not allowed to use 2-dim syntax :-((

Is there no other way ?

thnx
Chris
 
I

Ivan Korotkov

Try this:
int** arr2 = new int*[2];
for(int i=0; i<2;i++)
arr2 = new int[3];


Or better this:

int (*pi)[3] = new int[2][3];

The former method loses in performance (because each array access needs
two derefernces). The latter one doesn't, it's just the same as { int
pi[2][3]; }
 

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
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top