2002 2003 2004 2005 <2006> 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 | Index | 2002 2003 2004 2005 <2006> 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 |
<== Date ==> | <== Thread ==> |
---|
Subject: | Re: C strict aliasing rules |
From: | Andrew Johnson <[email protected]> |
To: | Eric Norum <[email protected]> |
Cc: | Core talk list <[email protected]> |
Date: | Mon, 27 Nov 2006 12:23:14 -0600 |
Eric Norum wrote:
static inline void writethree(int *p) { short *palias = (short *)p; *palias = 3; } int testalias() { int value = 12345678; writethree(&value); return value; }One might expect that the value returned from testalias() would be 12345678 with either its most-significant 16 bits or least- significant 16 bits set to 3 (on most architectures)..... One would be wrong, though.
I don't think life is quite as bad as it might seem; if you take off the "static inline" keywords from writethree(), I suspect the optimizer may not be able to be quite as aggressive in throwing out code - please experiment and respond to that suggestion.
Since we don't (shouldn't?) have any C routines in base that are marked as "static inline", I don't think our problems are as large as you make out them to be. I suspect the rules for C++ are subtly different, so it would also be interesting to see what using the C++ compiler generates.
- Andrew -- There is considerable overlap between the intelligence of the smartest bears and the dumbest tourists -- Yosemite National Park Ranger