Good programming style: multidimensional arrays or pointer technique 
Author Message
 Good programming style: multidimensional arrays or pointer technique

Hi,

I like to ask some of the more experienced (I am not) programmers their
opinion on the
following:

I am given a pointer to a sequential storaged set of numbers which
actually
consists of a three-dimensional array. I am writing a simple program
which
uses this array. Currently I do not use an array technique like assesing
an
element like

array[k][l][m]      0 < l < L, 0 < m < M etc

but use something like

*(ptr_to_array + k*L*M + l*M +m)

In other words I havent taken the effort to transform my 1-D pointer to
a
multidimensional array. Can somone comment on:

1) whether you consider this bad programming style?
2) do you expect differences in performance, i.e. speed?

    Thanks very much!

    Jeroen



Sun, 21 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique
IHMO, both approaches are equivalent, as foo[a][b][c] is actually the same
as foo[a+ASIZE*b+ASIZE*BSIZE*c].


Sun, 21 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique
Jeroen Groenenboom a crit dans le message

Quote:

>*(ptr_to_array + k*L*M + l*M +m)

This technique allows versatility and reusability. I use it (in FSM by
example) but the details are hidden (wrapped) it in some c-object.

typedef
{
   char ***p;
   int k;
   int l;
   int m;

Quote:
}sXX;

{
sXX *pxx = xx_create(K,L,M);
   data = xx_get(pxx);
   xx_put(pxx,data);
   xx_destroy(pxx);
   pxx=NULL;

Quote:
}

etc...

--
HS
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
About gets() and fflush(stdin) :
- "Even Buffy cannot slay the resulting RhinoDaemons."
Martin Ambuhl clc.



Sun, 21 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique


Quote:
>Hi,

>I like to ask some of the more experienced (I am not) programmers their
>opinion on the
>following:

>I am given a pointer to a sequential storaged set of numbers which
>actually
>consists of a three-dimensional array. I am writing a simple program
>which
>uses this array. Currently I do not use an array technique like assesing
>an
>element like

>array[k][l][m]      0 < l < L, 0 < m < M etc

>but use something like

>*(ptr_to_array + k*L*M + l*M +m)

>In other words I havent taken the effort to transform my 1-D pointer to
>a
>multidimensional array. Can somone comment on:

>1) whether you consider this bad programming style?

Your methos is a lot more difficult to read so from that perspective,
yes, it is bad style.

Quote:
>2) do you expect differences in performance, i.e. speed?

If L and M are compile time constants (i.e. the compiler knows their values
at compile time) and array and ptr_to_array are approriately typed possibly
not, but it depends on how the compiler you are using chooses to implement
it. Do you have any particular reason not to use a "three-dimensional"
array?

--
-----------------------------------------


-----------------------------------------



Sun, 21 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique


Quote:
>IHMO, both approaches are equivalent, as foo[a][b][c] is actually the same
>as foo[a+ASIZE*b+ASIZE*BSIZE*c].

Not with the same thing to the left of the [. The first and second cases
require that foo have different types for example

   TYPE foo[ASIZE][BSIZE[CSIZE];

vs.

   TYPE bar[ASIZE*BSIZE*CSIZE];

These are very different types.

--
-----------------------------------------


-----------------------------------------



Sun, 21 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique

Quote:



> >Hi,

> >I like to ask some of the more experienced (I am not) programmers their
> >opinion on the
> >following:

> >I am given a pointer to a sequential storaged set of numbers which
> >actually
> >consists of a three-dimensional array. I am writing a simple program
> >which
> >uses this array. Currently I do not use an array technique like assesing
> >an
> >element like

> >array[k][l][m]      0 < l < L, 0 < m < M etc

> >but use something like

> >*(ptr_to_array + k*L*M + l*M +m)

> >In other words I havent taken the effort to transform my 1-D pointer to
> >a
> >multidimensional array. Can somone comment on:

> >1) whether you consider this bad programming style?

> Your methos is a lot more difficult to read so from that perspective,
> yes, it is bad style.

> >2) do you expect differences in performance, i.e. speed?

> If L and M are compile time constants (i.e. the compiler knows their values
> at compile time) and array and ptr_to_array are approriately typed possibly
> not, but it depends on how the compiler you are using chooses to implement
> it. Do you have any particular reason not to use a "three-dimensional"
> array?

Currently my subroutine is transparent for both two and three dimensional
arrays
as if the two-dimensional arrays has a third dimension sized one. As I am an
old
fortran programmer i was afraid it was not possible to declare obtain an
array like array[512][400][1] for example. I think I better rewrite my program
with a real array to improve readability. Thanks

    Jeroen

- Show quoted text -

Quote:

> --
> -----------------------------------------


> -----------------------------------------



Mon, 22 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique
Quote:

> IHMO, both approaches are equivalent, as foo[a][b][c] is actually the same
> as foo[a+ASIZE*b+ASIZE*BSIZE*c].

That's only true when the first two indices have a fixed size
e.g. double foo[10][10][10]
foo[i][j][k]  is equiv. with   *(foo + 10*10*i + 10*j + k)
foo can also be defined as a dynamic array which results in a totally
different meaning
e.g double ***foo;
foo[i][j][k]  is equiv. with   *(*(*(foo + i) + j) + k)

Bernhard

--
------------------------------------------------------------------------------



Mon, 22 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique
Quote:
> IHMO, both approaches are equivalent, as foo[a][b][c] is
actually the same
> as foo[a+ASIZE*b+ASIZE*BSIZE*c].

Shouldn't this be foo[c+CSIZE*b+BSIZE*CSIZE*a]?

--
  This answer is courtesy of QuestionExchange.com
  http://www.questionexchange.com/showUsenetGuest.jhtml?ans_id=7475&cus...



Fri, 26 Apr 2002 03:00:00 GMT  
 Good programming style: multidimensional arrays or pointer technique
Quote:
> Hi,

> I like to ask some of the more experienced (I am not)
programmers their
> opinion on the
> following:

> I am given a pointer to a sequential storaged set of numbers
which
> actually
> consists of a three-dimensional array. I am writing a simple
program
> which
> uses this array. Currently I do not use an array technique
like assesing
> an
> element like

> array[k][l][m]      0 < l < L, 0 < m < M etc

> but use something like

> *(ptr_to_array + k*L*M + l*M +m)

> In other words I havent taken the effort to transform my 1-D
pointer to
> a
> multidimensional array. Can somone comment on:

> 1) whether you consider this bad programming style?

Yes, but if the code is one-use then I wouldn't sweat it.
If you ever plan to re-use or extend the code the array
syntax is easier to maintain and is self-documenting.
Quote:
> 2) do you expect differences in performance, i.e. speed?

They should be equivilent.

Quote:

>     Thanks very much!

>     Jeroen

--
  This answer is courtesy of QuestionExchange.com
  http://www.questionexchange.com/showUsenetGuest.jhtml?ans_id=7273&cus...


Fri, 26 Apr 2002 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Multidimensional arrays - what's it good for?

2. Pointers and multidimensional arrays

3. pointers to multidimensional array

4. Passing a multidimensional array with pointers

5. pointer to multidimensional array

6. pointer to a multidimensional array

7. Howard is confused: Multidimensional array of pointers to char

8. Passing and Returning multidimensional Arrays using pointers

9. good CSocket programming style

10. style, technique, ..etc

11. style, technique, ..etc

12. Programming style with arrays, summary

 

 
Powered by phpBB® Forum Software