home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!spool.mu.edu!sgiblab!news.kpc.com!kpc!hollasch
- From: hollasch@kpc.com (Steve Hollasch)
- Newsgroups: comp.lang.c
- Subject: Re: Accessing a dynamical 3-D field
- Summary: Multi-dimensional array element access
- Message-ID: <1992Dec29.190722.20859@kpc.com>
- Date: 29 Dec 92 19:07:22 GMT
- References: <1992Dec29.142134.25641@Informatik.TU-Muenchen.DE>
- Sender: usenet@kpc.com
- Organization: Kubota Pacific Computer, Inc.
- Lines: 32
-
- backscha@Informatik.TU-Muenchen.DE (Martin Backschat) writes:
- |
- | In my program I use 3-D fields of doubles with variable bounds.
- | This field is implemented as an array of doubles which is allocated
- | dynamically with malloc.
- | To access for example the [i][j][k] component in the field f of dimension
- | U,V,W I have to write f[i*V*W + j*W + k].
- |
- | Is it somehow possible to declare and allocate a 3-D field u (of doubles) with
- | variable bounds, allocated by malloc, in such a way that it can be accessed
- | with u[i,j,k] (with type casting)?
-
- Quite possibly, but to what advantage? When the compiler performs a
- mutli-dimensional array access, it does this exact computation to yield the
- address of the element in question. There are _some_ CPU's that have
- instructions to handle (non-general) 2D arrays, but I don't think there are
- many (if any) CPU's that have a single instruction to do a 3D array access.
-
- So, if you WERE able to typecast this to a multi-dimensional array (I
- tried to find a way but gave up), the compiler would still generate this
- code. What I would do is to just write a macro, e.g.:
-
- #define FIELD(array,i,j,k) \
- array [((((i) * V) + (j)) * W) + (k)]
-
- So then you could just do "FIELD(f,i,j,k)" to refer to a given element.
- Note the way that I computed the index takes one less multiply than the
- formula you listed.
-
- ______________________________________________________________________________
- Steve Hollasch Kubota Pacific Computer, Inc.
- hollasch@kpc.com Santa Clara, California
-