[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Converting an array to a multidimensional one

Slain

11/19/2008 2:15:00 AM

I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this

In the .h file
int *x;

in a initialize function:
x = new int[$Row_Length];

Now I need the x to be able to point to a multidimensional array
I would ahve been fine, with something like
int (*x)[Column_Length] = new int [Row_Length][Column_Length];

But since my variable x needs to be declated in the header file, I am
having some problems compiling. Can some one explain me how to declare
and initialize?

Thank you
9 Answers

Victor Bazarov

11/19/2008 2:36:00 AM

0

Slain wrote:
> I need to convert a an array to a multidimensional one. Since I need
> to wrok with existing code, I need to modify a declaration which looks
> like this
>
> In the .h file
> int *x;
>
> in a initialize function:
> x = new int[$Row_Length];
>
> Now I need the x to be able to point to a multidimensional array
> I would ahve been fine, with something like
> int (*x)[Column_Length] = new int [Row_Length][Column_Length];
>
> But since my variable x needs to be declated in the header file, I am
> having some problems compiling. Can some one explain me how to declare
> and initialize?

Have you tried looking in the FAQ?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Sam

11/19/2008 2:43:00 AM

0

Slain writes:

> I need to convert a an array to a multidimensional one. Since I need
> to wrok with existing code, I need to modify a declaration which looks
> like this
>
> In the .h file
> int *x;
>
> in a initialize function:
> x = new int[$Row_Length];
>
> Now I need the x to be able to point to a multidimensional array
> I would ahve been fine, with something like
> int (*x)[Column_Length] = new int [Row_Length][Column_Length];
>
> But since my variable x needs to be declated in the header file, I am
> having some problems compiling. Can some one explain me how to declare
> and initialize?

In C++ proper, you should be using std::vector, rather than old-fashioned C
arrays. Your declaration should be:

std::vector<std::vector<int> > x;

Then:

x.resize(4);

Four rows.

x[0].resize(3);
x[1].resize(3);
x[2].resize(3);
x[3].resize(3);

Each row is initialized to a three-element array, giving you an
two-dimensional array of four rows and three columns, accessed as:

x[row][col];


Salt_Peter

11/19/2008 4:10:00 AM

0

On Nov 18, 9:14 pm, Slain <Slai...@gmail.com> wrote:
> I need to convert a an array to a multidimensional one. Since I need
> to wrok with existing code, I need to modify a declaration which looks
> like this
>
> In the .h file
> int *x;
>
> in a initialize function:
> x = new int[$Row_Length];
>
> Now I need the x to be able to point to a multidimensional array
> I would ahve been fine, with something like
> int (*x)[Column_Length] = new int [Row_Length][Column_Length];
>
> But since my variable x needs to be declated in the header file, I am
> having some problems compiling. Can some one explain me how to declare
> and initialize?
>
> Thank you

Use a vector of vectors instead, much easier and it brings many
dividends (its dynamic, easy to extend - like providing iterators to
the interface). Instead of dealing with heap allocated pointers that
blindly point to whatever in an exception-unsafe way, deal with
objects instead.

// array2d.h, missing include guard

#include <vector>

class Array2D
{
typedef std::vector< int > VecN;
// members
std::vector< VecN > m_vvn; // 2D array
public:
// ctors
Array2D(std::size_t, std::size_t);
Array2D(std::size_t, std::size_t, const int);
// member functions
std::size_t size() const { return m_vvn.size(); }
VecN operator[](std::size_t index) { return m_vvn[index]; }
void display() const;
};

// array2d.cpp

#include <iostream>
#include <algorithm>
#include <iterator>
#include "array2d.h"

Array2D::Array2D(std::size_t rows, std::size_t cols)
: m_vvn(rows, std::vector< int >(cols)) { }

Array2D::Array2D(std::size_t rows, std::size_t cols, const int n)
: m_vvn(rows, std::vector< int >(cols, n)) { }

void Array2D::display() const
{
typedef std::vector< VecN >::const_iterator VIter;
for(VIter it = m_vvn.begin(); it != m_vvn.end(); ++it)
{
std::copy( (*it).begin(),
(*it).end(),
std::ostream_iterator< int >(std::cout, " ") );
std::cout << std::endl;
}
}

// main.cpp
#include "array2d.h"

int main()
{
Array2D a2d(4, 4, 99);
std::cout << "rows: " << a2d.size() << std::endl;
std::cout << "columns: " << a2d[0].size() << std::endl;
a2d.display();
}

/*
rows: 4
columns: 4
99 99 99 99
99 99 99 99
99 99 99 99
99 99 99 99
*/

Maxim Yegorushkin

11/19/2008 9:12:00 AM

0

On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:
> I need to convert a an array to a multidimensional one. Since I need
> to wrok with existing code, I need to modify a declaration which looks
> like this
>
> In the .h file
> int *x;

No need to modify the declaration. Multidimensional arrays in C++ are
stored as one-dimensional arrays anyway.

What you need to modify is how you calculate one-dimensional index.

> in a initialize function:
> x = new int[$Row_Length];

$Row_Length - is that a shell or Perl variable in here? ;)

> Now I need the x to be able to point to a multidimensional array
> I would ahve been fine, with something like
> int (*x)[Column_Length] = new int [Row_Length][Column_Length];
>
> But since my variable x needs to be declated in the header file, I am
> having some problems compiling. Can some one explain me how to declare
> and initialize?

You allocate your two-dimensional array like this:

int* x = new int[Row_Length * Column_Length];

And index into it like this:

int row, col; // initialised elsewhere
// access an element at x[row][col]
int& elem = x[row * Column_Length + col];

--
Max

James Kanze

11/19/2008 9:48:00 AM

0

On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
> On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:

> > I need to convert a an array to a multidimensional one.
> > Since I need to wrok with existing code, I need to modify a
> > declaration which looks like this

> > In the .h file
> > int *x;

Note that if this .h file is included in more than one file,
you'll get undefined behavior (and normally, multiple definition
errors when linking).

> No need to modify the declaration. Multidimensional arrays in
> C++ are stored as one-dimensional arrays anyway.

That's only true in the most superficial sense. You can't
access a multidimensional array as a one-dimensional array; the
two are different things.

(Formally speaking, of course, C++ doesn't have multidimensional
arrays. But it allows arrays of any type, including array
types, and an array of arrays works pretty much like a
multidimensional array for most things.)

> What you need to modify is how you calculate one-dimensional
> index.

> > in a initialize function:
> > x = new int[$Row_Length];

> $Row_Length - is that a shell or Perl variable in here? ;)

Or a typo; he uses Row_Length without the $ later.

> > Now I need the x to be able to point to a multidimensional
> > array I would ahve been fine, with something like
> > int (*x)[Column_Length] = new int [Row_Length][Column_Length];

> > But since my variable x needs to be declated in the header
> > file, I am having some problems compiling. Can some one
> > explain me how to declare and initialize?

> You allocate your two-dimensional array like this:

> int* x = new int[Row_Length * Column_Length];

> And index into it like this:

> int row, col; // initialised elsewhere
> // access an element at x[row][col]
> int& elem = x[row * Column_Length + col];

That's not a two dimensional array; that's just a method of
simulating one. While there are definitely cases where this
approach is recommended (or even necessisary), if his dimensions
(or at least Column_Length) is a constant, he can also write:
extern int (*x)[ Column_Length ] ;
in the header, and use
int (*x)[ Column_Length ] = new[ Row_Length ][ Column_Length ] ;
to initialize it.

Of course, a better solution might be to define a Matrix class,
and use that. With an implementation based on std::vector.
(Probably a one dimensional vector, calculating the indexes as
you described.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34

Maxim Yegorushkin

11/19/2008 10:22:00 AM

0

On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> wrote:
>
> > On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:
> > > I need to convert a an array to a multidimensional one.
> > > Since I need to wrok with existing code, I need to modify a
> > > declaration which looks like this
> > > In the .h file
> > > int *x;
>
> Note that if this .h file is included in more than one file,
> you'll get undefined behavior (and normally, multiple definition
> errors when linking).
>
> > No need to modify the declaration. Multidimensional arrays in
> > C++ are stored as one-dimensional arrays anyway.
>
> That's only true in the most superficial sense.  You can't
> access a multidimensional array as a one-dimensional array; the
> two are different things.

Yes, you can:

int(*x)[Cols] = new int[Rows][Cols];
// let's represent it as a plain array
int* y = x[0] + 0;

> (Formally speaking, of course, C++ doesn't have multidimensional
> arrays.  But it allows arrays of any type, including array
> types, and an array of arrays works pretty much like a
> multidimensional array for most things.)

In C++ one-dimensional and multi-dimensional arrays are different
names for the same thing - a contiguous block of memory. Using
language constructs one can view that block as a one-dimensional or
multi-dimensional array. Nevertheless, it is still fundamentally the
same thing in C and C++.

--
Max

Slain

11/19/2008 9:44:00 PM

0

On Nov 19, 5:22 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
> On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.com> wrote:
>
>
>
>
>
> > On Nov 19, 10:12 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> > wrote:
>
> > > On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:
> > > > I need to convert a an array to a multidimensional one.
> > > > Since I need to wrok with existing code, I need to modify a
> > > > declaration which looks like this
> > > > In the .h file
> > > > int *x;
>
> > Note that if this .h file is included in more than one file,
> > you'll get undefined behavior (and normally, multiple definition
> > errors when linking).
>
> > > No need to modify the declaration. Multidimensional arrays in
> > > C++ are stored as one-dimensional arrays anyway.
>
> > That's only true in the most superficial sense.  You can't
> > access a multidimensional array as a one-dimensional array; the
> > two are different things.
>
> Yes, you can:
>
>     int(*x)[Cols] = new int[Rows][Cols];
>     // let's represent it as a plain array
>     int* y = x[0] + 0;
>
> > (Formally speaking, of course, C++ doesn't have multidimensional
> > arrays.  But it allows arrays of any type, including array
> > types, and an array of arrays works pretty much like a
> > multidimensional array for most things.)
>
> In C++ one-dimensional and multi-dimensional arrays are different
> names for the same thing - a contiguous block of memory. Using
> language constructs one can view that block as a one-dimensional or
> multi-dimensional array. Nevertheless, it is still fundamentally the
> same thing in C and C++.
>
> --
> Max- Hide quoted text -
>
> - Show quoted text -

Thanks Max and James and the others!!!
We don't ahve STL's :(

I would eprsonally want to go ahead with the single dimensional
approach which max suggested, but that is becase I am an electrical
engineer. I think since this fits into existing code, I would have it
access elements much like a two dimensional array. It would be much
easier for the future person dealing with it.

The Row_Length was just means to provide that a fixed value will go in
there.

I think I will try James approach. Thank you guys!!! I might come abck
with more questions :)

Thanks a lot

James Kanze

11/20/2008 8:43:00 AM

0

On Nov 19, 11:22 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
> On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.com> wrote:
> > On Nov 19, 10:12 am, Maxim Yegorushkin
> > <maxim.yegorush...@gmail.com> wrote:

> > > On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:
> > > > I need to convert a an array to a multidimensional one.
> > > > Since I need to wrok with existing code, I need to modify a
> > > > declaration which looks like this
> > > > In the .h file
> > > > int *x;

> > Note that if this .h file is included in more than one file,
> > you'll get undefined behavior (and normally, multiple
> > definition errors when linking).

> > > No need to modify the declaration. Multidimensional arrays
> > > in C++ are stored as one-dimensional arrays anyway.

> > That's only true in the most superficial sense. You can't
> > access a multidimensional array as a one-dimensional array;
> > the two are different things.

> Yes, you can:

> int(*x)[Cols] = new int[Rows][Cols];
> // let's represent it as a plain array
> int* y = x[0] + 0;

I'm not quite sure what the + 0 is doing there; it changes
absolutely nothing. But all you've got is still a pointer to
the first element of x[0]; an expression like y[Rows+1] is
undefined behavior.

> > (Formally speaking, of course, C++ doesn't have
> > multidimensional arrays. But it allows arrays of any type,
> > including array types, and an array of arrays works pretty
> > much like a multidimensional array for most things.)

> In C++ one-dimensional and multi-dimensional arrays are
> different names for the same thing - a contiguous block of
> memory.

That's simply false. In C++, arrays have a type; they're not
just a block of (raw) memory. And that type includes the
dimension.

> Using language constructs one can view that block as a
> one-dimensional or multi-dimensional array.

Not without invoking undefined behavior.

> Nevertheless, it is still fundamentally the same thing in C
> and C++.

That's true. The C standard was carefully written to allow
bounds checking. I only know of one implementation which ever
did it, but the standard is clear; it's legal, and anything that
would cause a bounds check error is undefined behavior.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Maxim Yegorushkin

11/20/2008 10:16:00 AM

0

On Nov 20, 8:43 am, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 19, 11:22 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> wrote:
>
> > On Nov 19, 9:47 am, James Kanze <james.ka...@gmail.com> wrote:
> > > On Nov 19, 10:12 am, Maxim Yegorushkin
> > > <maxim.yegorush...@gmail.com> wrote:
> > > > On Nov 19, 2:14 am, Slain <Slai...@gmail.com> wrote:
> > > > > I need to convert a an array to a multidimensional one.
> > > > > Since I need to wrok with existing code, I need to modify a
> > > > > declaration which looks like this
> > > > > In the .h file
> > > > > int *x;
> > > Note that if this .h file is included in more than one file,
> > > you'll get undefined behavior (and normally, multiple
> > > definition errors when linking).
> > > > No need to modify the declaration. Multidimensional arrays
> > > > in C++ are stored as one-dimensional arrays anyway.
> > > That's only true in the most superficial sense.  You can't
> > > access a multidimensional array as a one-dimensional array;
> > > the two are different things.
> > Yes, you can:
> >     int(*x)[Cols] = new int[Rows][Cols];
> >     // let's represent it as a plain array
> >     int* y = x[0] + 0;
>
> I'm not quite sure what the + 0 is doing there;

It is a shortcut for:

int* y = &x[0][0];

> it changes absolutely nothing.
> But all you've got is still a pointer to
> the first element of x[0];

You are right that x[0] is sufficient.

> an expression like y[Rows+1] is undefined behavior.

It is just meaningless.

> > > (Formally speaking, of course, C++ doesn't have
> > > multidimensional arrays.  But it allows arrays of any type,
> > > including array types, and an array of arrays works pretty
> > > much like a multidimensional array for most things.)
> > In C++ one-dimensional and multi-dimensional arrays are
> > different names for the same thing - a contiguous block of
> > memory.
>
> That's simply false.  In C++, arrays have a type; they're not
> just a block of (raw) memory.  And that type includes the
> dimension.

There are two separate issues: type and binary layout.

You are quite right that the types are distinct and unrelated
according to the standard, and thus casting is formally undefined
behaviour.

The binary layout is quite a different story. Size of an object is
always a multiple of its alignment. The size is defined this way, so
that when objects are stored in an array there is no padding between
the objects. Thus, the size of a one-dimensional array is nothing more
than the size of an element multiplied by the number of elements.

In a multi-dimensional array the elements are arrays. Due to the
requirement that there be no padding between the elements of an array,
there is no padding between elements-arrays of a multi-dimensional
array. Thus, multi-dimensional arrays can not be stored any other way,
but exactly as a single-dimensional array with the number of elements
equal to the total number of elements of the multi-dimensional array.

Essentially, the standard implicitly guarantees that the binary
layouts of arrays with the same underlying object type but with
different dimensions are the same as long as the total number of the
objects is the same.

> > Using language constructs one can view that block as a
> > one-dimensional or multi-dimensional array.
>
> Not without invoking undefined behavior.

On one hand the standard says that the casting between arrays of
different dimensions is undefined behaviour, on the other hand it
provides the aforementioned binary layout guarantees. In my opinion,
it is an underspecification or inconsistency of the standard.

--
Max