Common gcc warnings and their solutions

array subscript has type 'char'

When char ch; is used as array subscript, the index will be negative for chars above ascii(127).
Instead of

array[ch] = v;

#include <inline.h> and then use

array[safeCharIndex(c)] = v;

This is safe w/o causing runtime overhead.

dereferencing type-punned pointer will break strict-aliasing rules

This is a quite severe warning, cause the code it warns about will most likely show unwanted behavior. It's caused by strict-aliasing, a compiler optimization feature only enabled when using optimization level 2 or higher. ARB will be affected in the NDEBUG version - to see this warning you'll have to compile ARB with DEBUG=0 in config.makefile.

This happens only if the casted types are very different, i.e. casting int to long or double to float should be unaffected.

A simple wrong example:

char *c = strdup("whatever");
doSthWith((long*)&c); // it's undefined whether the string-address or some random value is passed to the function
// after the call it's undefined whether c contains the 
// value to which it has possibly been changed by 'doSthWith'

The correct way to do it is:

union { char *c; long l; } val;
val.c = strdup("whatever");

Another "solution" is to pass the value to a function, which acts as optimization-fixpoint:

void wrapper(char **cp) { doSthWith((long*)cp); }
char *c = strdup("whatever");
Last modified 15 years ago Last modified on Oct 29, 2009, 10:11:17 AM