[Beowulf] Stroustrup regarding multicore
Michael H. Frese
Michael.Frese at NumerEx-LLC.com
Tue Aug 26 15:13:09 PDT 2008
My first comment had nothing to do with whether C's 2-d arrays are
allocated contiguously in memory. I assume that they are, as was
implicit in my remark about "doing the stepping by hand."
My comment has to do with the assembly code that the C compiler
generates to access an element of the array. My much dog-eared
edition of K&R (1978) -- with a fair bit of faded yellow
highlighting, all mine, dating from 1983 -- says very clearly in the
section "Multi-dimensional Arrays" that "by definition, a
two-dimensional array is really a one-dimensional array, each of
whose elements is an array." Any compiler writer may thus choose to
store the addresses of the beginnings of the rows of an array in a
one-dimensional array, then recall and de-reference those pointers to
get the values of the two-dimensional array. In particular, this
must be valid:
*(a+3) = 7 ;
A C compiler must be perfectly happy to printf("a=%d",a), or
for that matter (a). You may consider this an advantage. I am
not so sure.
I stipulate that the compiler writer MAY choose to compute a -- a
pointer to real -- every time it appears, but I doubt there's
language in the standard that requires it, since only hardware
characteristics say which is more efficient.
As for my final comment, Appendix A of K&R, "C Reference Manual" says
clearly that the [ ] in the array declarator may be empty but if not
it must contain a constant expression "whose value is determinable at
compile time." Clearly, one can not separately compile a function
that uses multidimensional array syntax -- not pointer syntax -- to
return a specified value of an arbitrarily dimensioned array. That
can be easily done in Fortran, since the array dimensions may be
dummy arguments to the subroutines. Recognizing them as variable
array dimensions, the compiler sets aside memory locations for their
values, and writes code to set those locations to the values pointed
to by the dummy arguments on entry to the subroutine. The Fortran
standard requires this arcane behavior, because it prohibits the real
dimensions from changing between entry and exit, though the reason
for that is far from clear to me. It's quite amusing really, that
since the real array dimensions are decoupled from the dummy argument
value, they are effectively called by value.
Regarding RGB's lengthy response, I agree whole-heartedly that C
should not be expected to handle arrays perfectly, and that each
language has its own field of best application, such as C++ to GUI's,
C for system interfaces, and Fortran for array number crunching. For
such things wrappers are made.
Sorry for being so obtuse originally.
At 09:05 AM 8/26/2008, you wrote:
>"Michael H. Frese" <Michael.Frese at NumerEx-LLC.com> writes:
> > C is not much better. I once worked a young computational programmer
> > for almost a week to get him to prove to himself that a C source
> > program couldn't walk through a 2-d array the hard way as fast as a
> > Fortran source program unless the stepping was coded by hand.
>I don't understand what that means. I've been programming in C for
>about 25 years, and I have known Fortran since the mid-1970s.
> > He didn't believe that a 2-d array in C is syntactically a 1-d array
> > of pointers to 1-d arrays,
>He was right. You are just plain wrong.
>allocates 100 consecutive addresses in memory.
>In case you don't believe me, try out the following program:
>int main(int argc, char **argv)
> int i, j;
> for (i = 0; i < 10; i++)
> for (j = 0; j < 10; j++)
> printf("%d\n", &(foo[i][j]));
>It will print 100 consecutive integers, the addresses of the character
> > and the row pointers must be fetched from memory!
>I call bull. That's just totally false. You clearly don't know how C
> > And separate compilation of functions with variable array
> > dimensions? I hear echoes of Kernighan and Ritchie laughing with each
> > other "We don't need no steenking libraries with execution-time array
> > dimensioning! We're system programmers here! Besides, if somebody
> > needs that they'll use Fortran."
>Er, you can do that, too. Works just fine. What *are* you talking
>Beowulf mailing list, Beowulf at beowulf.org
>To change your subscription (digest mode or unsubscribe) visit
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beowulf