🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2619

Re: Multidimeensional Arrays vs. pointers

#2619
From: Chris Torek
Date: Thu, 18 Apr 1996 12:37
67 lines
3129 bytes
In article <4l1ieh$q0q@solutions.solon.com> Eric Winter
<winter@ukraine.corp.mot.com> wrote:
>Is it possible to cast object into a multidimensional array?

(No, casts can only produce values, and with one exception, there
are no array values.)

>Malloc a multidim. array?

Yes -- but first you need to define a `multidimensional array'.
(What C has is probably best called `arrays of arrays'.  These
are isomorphic to multidimensional arrays, but thinking of them
as ordinary multidimensional arrays will lead to confusion.)

The comp.lang.c FAQ has an extensive discussion of this.

>... Some people are religious about flexibility and use pointers, and
>arrays of pointers to implement multidimensional arrays, others coming
>from the FORTRAN world like clarity and define everything at compile
>time and publish dimensions in their #include's. ... The problem is
>when the two collide its like matter and anti-matter.

The FAQ provides a method for implementing a row-vector interface to
a flat-memory matrix.  This may (depending on the code in question)
allow either access method.  Because the pointers *are* flexible, you
simply point each one to the next row in the flat-memory area.  This
even works if the flat-memory area arose from an ordinary variable
declaration, e.g.,

	double mat[M][N];


>I would also love to hear exactly how a multidim. array is actually
>stored i.e. there must be some overhead stating what the innermost
>dimensions are.  How is that stored.  Is it physically stored as
>a header to the data?

There are no guarantees here, as implementations are free to add
any extra frills/features they like.  However, for the most part,
this overhead does exist, but ONLY AT COMPILE TIME, and the compiler
itself is the only place in which the dimensions are stored.  (This
alone makes arrays-of-arrays fundamentially different from a typical
language's variable-sized multidimensional arrays.)  The memory
itself is allocated in a `flat' area, much as if you had written:

	double mat0[M * N];

and then `mat[i][j]' is located in much the same way as if you had
written:

	mat0[i * N + j]

In most implementations, you can replace:

	mat[i][j]

with:

	mat[0][i * N + j]

everywhere with no change in behavior, although the C Standard says
that you must not do so (for instance, an implementation may, but
is not required to, verify that each subscript is in range; here,
whenever i>0, the check would fail for all j>=0).
-- 
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA	Domain:	torek@bsdi.com	+1 510 234 3167
Technical Details
Message-ID: <4l5d2q$22v@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4khlpr$t5g@solutions.solon.com> <4l1ieh$q0q@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Chris Torek <torek@elf.bsdi.com>
Newsgroups: comp.lang.c.moderated
Subject: Re: Multidimeensional Arrays vs. pointers
Date: 18 Apr 1996 07:37:46 -0500
Organization: Berkeley Software Design, Inc.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4l5d2q$22v@solutions.solon.com>
References: <4khlpr$t5g@solutions.solon.com> <4l1ieh$q0q@solutions.solon.com>
Reply-To: torek@bsdi.com
NNTP-Posting-Host: solutions.solon.com