Multidimensional Static Array Initialization Follow-up

Chris Torek chris at mimsy.UUCP
Thu Aug 18 20:59:42 AEST 1988


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 a declaration
>like
>	
>	int foo[][] = { { 1, 2,  3,  4  },
>		 	{ 5, 8          },
>		 	{ 9, 10, 12     } };
>
>and have it do exactly the same thing without the bother of me having to
>know in advance what the largest number of elements to appear in any row
>is going to be?

Yes.  Consider the situation from the compiler's point of view,
and in particular, the case where the first row is not the longest:

	int foo[][?] = { { 1	      },
			 { 2, 3, 4, 5 },
			 ... };

If the `?' is filled in with (e.g.) 4, the compiler can generate assembly
or object code as:

		.dataseg
	foo_:
		.int	1
		.int	0
		.int	0
		.int	0
		.int	2
		.int	3
		.int	4
		.int	5

If, on the other hand, no value is supplied for `?', the compiler
must defer emission of any of these constants until it has deduced
the appropriate `?'-value.

There is a way to do this, if the assembler or object format is
powerful enough, viz:

		.dataseg
	foo_:
		.int	1
		.space	S0	# (assuming this zero-fills)
		.int	2
		.int	3
		.int	4
		.int	5
		.space	S1
		# etc
		.set	S0,6	# row 0 needs 3 ints, 2 bytes each
		.set	S1,0	# row 1 needs 0 ints
		# etc

>So, what I meant to ask in my original posting was why are things not
>implemented in this way?

Basically, it is to make one-pass compilers easier to write.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list