barts@tekcrl.CRL.TEK.COM (Bart Schaefer) (09/01/88)
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.
bill@proxftl.UUCP (T. William Wells) (09/08/88)
In article <3008@tekcrl.CRL.TEK.COM> barts@tekcrl.CRL.TEK.COM (Bart Schaefer) writes:
: Suppose I make the following declaration:
:
: [declaration omitted]
:
: 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];
No. If there is any possibility of overlap, you can be that some
compiler will move them in such a way as to make you miserable.
The new standard specifically states that the implementation does
not have to do such assignments correctly. See 3.3.16.1.
---
Bill
novavax!proxftl!bill