A minor clarification, unions are how you do safe type-punning.
When you write to something, make a pointer to it as another type and dereference that, the compiler (may or may not) detect it as a type-punned alias and warn you, and may or may not make working code.
The correct way is to create a union and put the value in one field and read it out of another field, unions are always treated as volatile and thus has no issues as it is never held in a register for
multiple operations (always stored in memory).
Note that the volatile keyword may also make type-punning safe, if you mark a variable as volatile that is… this predictably has a bad effect on the optimizer and is really intended for driver
programming (where you never want a memory mapped register to be cached by the optimizer) or thread synchronization (along with a bus locking operation such as you find in mutex and other thread
synchronization constructs).
Aliasing is when you access a variable’s contents as two different types, what passes for a C spec says that compilers never have to concern themselves with supporting aliasing, they can assume
nothing is aliased when optimizing, you can tell them to assume aliasing (gcc calls this -fno-strict-aliasing for example) but this hurts performance, so it’s far better to use unions for memory-type
conversions of that nature.On 05/14/2010 07:42 AM, Jonathan Dearborn wrote:
Just to be a bit less vague:
-Werror causes all warnings to be seen as errors. Warnings do not stop
you from compiling, but errors do. Making warnings act like errors
forces you to fix them and ensure proper code.
This particular error (though I’m not looking at the code) has to do
with type-punning, which is any method to defeat the strict type system
of the language. Commonly, this happens with C’s unions because they
are a somewhat strange construct. It’s like they can only hold one
variable at a time, so it is a naughty practice to access a variable in
the union that was not the most recently written one.
Jonny D
On Fri, May 14, 2010 at 6:51 AM, Lohitha R <lohitha.r at globaledgesoft.com <mailto:lohitha.r at globaledgesoft.com>> wrote:
Hi, Jonny D
Thank you.... Its working....
--
Lohitha R
Jonathan Dearborn wrote:
Try removing -Werror from your compile line.
Jonny D
On Fri, May 14, 2010 at 12:46 AM, Lohitha R <lohitha.r at globaledgesoft.com <mailto:lohitha.r at globaledgesoft.com>> wrote:
Hi,
I'm not able to compile the SDL_gui-0.10.3 successfully...
please guide me. I got the source from
http://rhk.dataslab.com/files/SDL_gui-0.10.3.tar.gz
I got the make error, it as shown below...:
c++ -DPACKAGE=\"SDL_gui\" -DVERSION=\"0.10.3\"
-DBUILDING_SDLGUI_DLL -g -O2 -Wall -Werror -I../include -g -O2
-I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -c Drawable.cc
-fPIC -DPIC -o .libs/Drawable.lo
cc1plus: warnings being treated as errors
Drawable.cc: In member function 'void
GUI_Drawable::SetStatusCallback(GUI_Callback*)':
Drawable.cc:232: warning: dereferencing type-punned pointer
will break strict-aliasing rules
make[1]: *** [Drawable.lo] Error 1
make[1]: Leaving directory
`/home/lohitha/untars/SDL_gui-0.10.3/src'
make: *** [all-recursive] Error 1
--
Lohitha R
_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
------------------------------------------------------------------------
_______________________________________________ SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier