Multidimensional Static Array Initialization Follow-up

Wonderly gregg at ihlpb.ATT.COM
Tue Aug 23 05:37:22 AEST 1988


>From article <8584 at ihlpb.ATT.COM>, by tainter at ihlpb.ATT.COM (Tainter):
> In article <13060 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
>> In article <2682 at jpl-devvax.JPL.NASA.GOV> timg at jpl-devvax.JPL.NASA.GOV
>>(Tim Graham) writes:
>>>For example ...
>>>  int foo[][4] = { { 1, 2,  3,  4  }, { 5, 8          }, { 9, 10, 12     } };
>>>... the uninitialized elements in the 3x4 array are filled with zeros.  Is
>>>it really that much harder for it to be possible to implement
>>>  int foo[][] = { { 1, 2,  3,  4  }, { 5, 8          }, { 9, 10, 12     } };
> 
>> Yes.  Consider the situation from the compiler's point of view,
>> and in particular, the case where the first row is not the longest:
> 
> It isn't just an issue of the initializer.  Consider code referencing the
> above entity foo.  As long as only one dimension is unknown C can generate
> code to manipulate the structure without requiring patching at link time.

This looks to be a simple ilef vector.  e.g.  one might construct the
equivalent by writing the following C code.

int **foo;

foo = malloc (3*sizeof(int*));
foo[0] = malloc (4*sizeof(int));
foo[0][0] = 1;
foo[0][1] = 2;
foo[0][2] = 3;
foo[0][3] = 4;
foo[1] = malloc (2*sizeof(int));
foo[1][0] = 5;
foo[1][1] = 8;
etc.

or in assembler (as the compiler sees it)

int foo[][]     Compiler says OHHHH there are missing dimensions, I
can't possibly know how to do the multiplication, so lets use pointers
instead.

foo	equ	$
	.addr	1$
	.addr	2$
	.addr	3$
1$	equ	$
	.long	1
	.long	2
	.long	3
	.long	4
2$	equ	%
	.long	5
	.long	8

Now there is some (possibly huge) amount of context that needs to be saved
in order to count the rows.  Alternatively the assembler could support
psect's by name that would allow the compiler to just call it as it sees it

foo	psect	foo
	.addr	1$
	psect	foo.
1$	equ	$
	.long	1
	.long	2
	.long	3
	.long	4
	psect	foo
	.addr	2$
	psect	foo.
2$	equ	$
	.long	5
	.long	8

or some slighty more coherent syntax.  This would save a lot of time in
building sparse matrices.  When referencing such a beast, the same
assumption as was made above should be made;  i.e. if there is more than
one missing dimension, each of those is accessed through a pointer from
the previous dimension.

Gregg Wonderly
AT&T Bell Laboratories                   DOMAIN: gregg at ihlpb.att.com
IH2D217 - (312) 979-2794                 UUCP:   att!ihlpb!gregg
-- 
Gregg Wonderly
AT&T Bell Laboratories                   DOMAIN: gregg at ihlpb.att.com
IH2D217 - (312) 979-2794                 UUCP:   ihnp4!ihlpb!gregg



More information about the Comp.lang.c mailing list