Re: help with type-punned warning please

From: Vijay Kumar R Zanvar (vijoeyz_at_hotpop.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 12:56:24 +0530


"David Ford" <david+challenge-response@blue-labs.org> wrote in message
news:pan.2004.02.26.04.59.47.581146@blue-labs.org...
> 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

This should fix the warnings:

#include <stdio.h>
#include <stdlib.h>

#define sfree(x) _internal_sfree((void **)&x)
#define _internal_sfree(x) do { \
        if ( (x) && *(x) ) \
        { \
            free(*(x)); \
            *(x)=NULL; \
        } \
    }while (0)

void somefunction()
{
    char *x = malloc(10);
    int *y = malloc(10);

    sfree(x);
    sfree(y);
}

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/


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)
  • Re: help with type-punned warning please
    ... > clean up my code so I don't get these warnings. ... > void somefunction() { ... > What's the best way of fixing my lack of clue? ...
    (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)
  • How to fix compiler warning
    ... 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 main{ ...
    (comp.lang.c)