C is not a rigorous language; C++ addresses some of the issues, but still drags along some of the dangerous constructs. I didn't like it when it first came out, and still don't, but it's unavoidable in the current marketplace.
-- Shmuel (Seymour J.) Metz http://mason.gmu.edu/~smetz3 ________________________________________ From: IBM Mainframe Assembler List <[email protected]> on behalf of Charles Mills <[email protected]> Sent: Saturday, September 12, 2020 10:00 PM To: [email protected] Subject: Re: Deep Cuts I believe the standard says you can only use the same value of a union that you set. Given union {int foo; int *bar;} myunion; it is not legal to say foo = something; something_else = bar; -- although if you can't do that, what is the point of a union, other than saving space? (Or yes, perhaps myunion is part of some larger struct and is an int in some circumstances and a pointer in others, so it is always one or the other.) > that pointers and integer might be totally different sizes And in fact are in AMODE 64, right? (I have not done any AMODE 64 C so I lose track. A size_t is 64 bits but an int is still 31/32, right?) I would think if you were going to design a rigorous language you would not have a memset() as part of the language or its included library. Memset implicitly treats its target as a union between whatever it actually is and an array of chars. So using memset kind of violates the restriction I speak of in the first paragraph. Charles -----Original Message----- From: IBM Mainframe Assembler List [mailto:[email protected]] On Behalf Of Thomas David Rivers Sent: Saturday, September 12, 2020 4:27 PM To: [email protected] Subject: Re: Deep Cuts Paul Gilmartin wrote: > > ... > > Maybe, if you overlay the pointer with an int, assigning zero could work, > > because zero addresses in pointer variables are not translated and > > dereferencing such pointer variables could still work? > > > Type punning. Most implementations state that the > effect of type punning is implementation-dependent > or unpredictable. > Type-punning _is_ "badness" for sure! Newer C standards allow it thru characters (which is an interesting change... perhaps because a "char" is basically a byte.) But - Gil is correct - in this case: union u { void *p; int i; } u; doing: u.i = 0; followed by: if(u.p == 0) { is no good and undefined. (This also completely neglects the idea that pointers and integer might be totally different sizes.) We can propose a different question that's along similar lines though. What if we have: struct s { void *p; int i; } s; ... memset(&s, 0, sizeof(s)); what is the value of 'p' then? It would be filled with 0 bytes, so then what would: if(s.p == 0) { mean? (See my other post...)
