Re: help with type-punned warning please

From: David Ford (david+challenge-response_at_blue-labs.org)
Date: 02/26/04


Date: Thu, 26 Feb 2004 02:23:52 -0500

On Wed, 25 Feb 2004 23:59:48 -0500, David Ford wrote:

> I have a macro that I use across the board for freeing ram. I'd like to
> clean up my code so I don't get these warnings.
>
> #define sfree(x) _internal_sfree((void **)&x)
> #define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } })
>
> void somefunction() {
>
> char *x = malloc(10);
> int *y = malloc(10);
>
> sfree(x);
> sfree(y);
> }
>
> results in:
>
> warning: dereferencing type-punned pointer will break strict-aliasing
> rules
>
> What's the best way of fixing my lack of clue?
>
> Thank you,
> David

Just for the curious, I've since changed my sfree() macro to the following:

#define sfree(x) \
({ \
        __typeof(&x) T = (&x); \
                                        \
        if ( T && *T ) { \
                free (*T); \
                *T = NULL; \
        } \
})



Relevant Pages

  • Re: help with type-punned warning please
    ... >> clean up my code so I don't get these warnings. ... >> void somefunction() { ... > which has two illegal instances of braced groups within expressions. ...
    (comp.lang.c)
  • Re: help with type-punned warning please
    ... > I have a macro that I use across the board for freeing ram. ... > clean up my code so I don't get these warnings. ... > void somefunction() { ...
    (comp.lang.c)
  • help with type-punned warning please
    ... I have a macro that I use across the board for freeing ram. ... clean up my code so I don't get these warnings. ... void somefunction() { ...
    (comp.lang.c)
  • Re: 2.6.0-test9-mm1 (compile stats)
    ... Building fs/adfs: clean ... Building drivers/net: 31 warnings, 0 errors ... Building drivers/media: 1 warnings, 0 errors ... drivers/ide/ide.c:2470: warning: implicit declaration of function ...
    (Linux-Kernel)
  • Re: 2.6.0-test9-mm2 (compile stats)
    ... Building fs/adfs: clean ... Building drivers/net: 31 warnings, 0 errors ... Building drivers/media: 1 warnings, 0 errors ... drivers/ide/ide.c:2470: warning: implicit declaration of function ...
    (Linux-Kernel)