Wall로 Build 를 하는데 아래와 같은 워닝이 발생해서
warning: dereferencing type-punned pointer will break strict-aliasing rules
구글링좀 하고, 고치기 귀찮아서 MakeFile만 수정해서 올림
연구원 씨가 관심 있으면 고치겠지.....
The following short examples demonstrate my problem:
----Exhibit A
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} item_t;
int return_item (void **item);
int return_item (void **item)
{
void *mem;
mem = malloc (1);
if (mem) {
*item = mem;
return 0;
}
else
return 1;
}
int main (int argc, char *argv[])
{
item_t *item;
if (return_item ((void **)&item) == 0) {
printf ("%p\n", item);
free (item);
}
return 0;
}
----Exhibit B
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} item_t;
int return_item (void **item);
int return_item (void **item)
{
void *mem;
mem = malloc (1);
if (mem) {
*item = mem;
return 0;
}
else
return 1;
}
int main (int argc, char *argv[])
{
item_t *item;
void *item_temp;
if (return_item (&item_temp) == 0) {
item = item_temp;
printf ("%p\n", item);
free (item);
}
return 0;
}
----
The code is a generic example of the problem. The real code that is
producing the problem is a hashing API which hashes (void *) and hence
uses (void **) as an out parameter type.
Exhibit A produces a warning as follows:
[nodbug:mato]$ gcc -O2 -Wall -o aliasing-test aliasing-test.c
aliasing-test.c: In function `main':
aliasing-test.c:28: warning: dereferencing type-punned pointer will break strict-aliasing rules
I'm using gcc version 3.3.5 (Debian 1:3.3.5-13) but the problem persists
even when tested with GCC 4.x on newer systems.
Exhibit B is my proposed "fix". Can anyone advise if the code in
Exhibit A is legitimate, i.e. whether or not it's really violating the
strict aliasing rules as defined by the C standard? If not, then I
guess the warning is spurious and I can safely use the fix.
If the code is in fact violating the standard then feel free to
enlighten me how to fix it, since it seems like a legitimate thing to do
:-)
Thanks very much for any help,
귀차나서 그대로 긁어 왔습니다.
참고 : http://gcc.gnu.org/ml/gcc-help/2006-08/msg00236.html
음 문제가 되면 삭제 하겠습니다 훗..