Maxim Yegorushkin
11/20/2008 10:16:00 AM
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