Overlapping assignments

Bart Schaefer barts at tekcrl.CRL.TEK.COM
Fri Sep 2 02:35:14 AEST 1988


Suppose I make the following declaration:

union node {
    struct cell {
	char tag;
	double val;
    } n_cell[2];
    struct vec {
	short count;
	struct cell args[3];
    } n_vec;
} some_node;

(This is actually a minimized version of a much more complex union, so
please don't tell me better ways I could have made that particular
declaration in order to avoid the question I'm about to ask.)

Given that declaration, is the following assignment guaranteed to work
for any (integer) values of i and j (assuming a compiler that supports
structure assignment)?

some_node.n_cell[i] = some_node.n_vec.args[j];

I *know* that I can't make *two* such assignments in succession for the
same union node and expect anything sensible to happen, but as it turns
out I never need to do more than one at a time.

My concern is that, for example, n_cell[0] may overlap n_vec.args[0] by
as much as 7 bytes (given 2-byte shorts and 8-byte doubles).  I realize
that it would be safer to declare a temporary struct cell and use two
assignments, but I would like to avoid that if possible, because the
actual struct involved is rather large and my stack space is very limited.

I need to compile this on at least 3 different machines, each with a
different OS and C compiler, none of which are draft-ANSI conformant.
Thanks in advance.



More information about the Comp.lang.c mailing list