runtime 'sizeof()'?

Robert Viduya robert at gitpyr.UUCP
Fri Jan 18 13:16:19 AEST 1985


[]

The other day I was modifying some source code when I came across something
that showed me one of the limitations of the sizeof operator.  Basically, I
had the following in a source file:

	extern struct s	a[];
		.
		.
		.
	    b = sizeof(a);
		.
		.
	
Needless to say, the compiler assigned 0 to b instead of the actual size of
the array.

Obviously, in order to handle things like this, C needs a runtime version
of sizeof.  I can see two ways of doing this.  One way is to have the size
of each variable stored as part of that variable (waste a bit of storage).
Another way is to implement it as a real function that 'knows' where all
the variables are and can compute the size of each (also can waste storage).

A third way, which I see to be the best, is to defer the evaluation of any
sizeof operation that references an external value to link/load time and put
a placeholder in its place.  The linker/loader would have to replace the
placeholder with the actual size of the external variable (since it knows
the size anyway).  This doesn't waste any unnecessary storage for 'hidden'
variables and also allows using the sizeof operator with external variables.

Comments anybody?

			robert
-- 
Robert Viduya
    Office of Computing Services
    Georgia Institute of Technology, Atlanta GA 30332
    Phone:  (404) 894-4669

...!{akgua,allegra,amd,hplabs,ihnp4,masscomp,ut-ngp}!gatech!gitpyr!robert
...!{rlgvax,sb1,uf-cgrl,unmvax,ut-sally}!gatech!gitpyr!robert



More information about the Comp.lang.c mailing list