Re: help with type-punned warning please
From: Vijay Kumar R Zanvar (vijoeyz_at_hotpop.com)
Date: 02/26/04
- Next message: David Ford: "Re: help with type-punned warning please"
- Previous message: Mohanasundaram: "c89 and c99"
- In reply to: David Ford: "help with type-punned warning please"
- Next in thread: David Ford: "Re: help with type-punned warning please"
- Reply: David Ford: "Re: help with type-punned warning please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: David Ford: "Re: help with type-punned warning please"
- Previous message: Mohanasundaram: "c89 and c99"
- In reply to: David Ford: "help with type-punned warning please"
- Next in thread: David Ford: "Re: help with type-punned warning please"
- Reply: David Ford: "Re: help with type-punned warning please"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|