Eliminating GCC 3.3 warning

Bob Friesenhahn <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.core
Message-ID <[email protected]>
GCC 3.3 warns like:

warning: dereferencing type-punned pointer will break strict-aliasing rules

for code like

  LiberateMemory((void **) &pixels);

When -Wall (enable all reasonable warnings) is applied.

This is quite annoying, and has to do with casting the address pointer
to a lesser type, which is apparently often a source of bugs.  I
haven't found a cast which makes the warning go away.

If I use

  #define LiberateMemory(memory) \
  { \
    assert(memory != 0); \
    void *mp=*memory; \
    if (mp != 0) LiberateMemory(&mp); \
    *memory=0; \
  }

  LiberateMemory(&pixels);

then there is no warning, but notice I have to remove the cast from
the LiberateMemory calls so there would be code changes in order to
avoid the warning.  Naturally, these calls are made all over the
place.  The need to cast the argument to LiberateMemory is actually
pretty annoying, so it may be that a macro would help.

Does anyone have a bright idea as to how to avoid this warning with
minimal changes?

Bob
======================================
Bob Friesenhahn
[email protected]
http://www.simplesystems.org/users/bfriesen



-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.