IT 취미생활.  
Front Page
Tag | Location | Media | Guestbook | Admin   
 
warning: dereferencing type-punned pointer will break strict-aliasing rules

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


음 문제가 되면 삭제 하겠습니다 훗..



BLOG main image
취미생활
 Notice
 Category
분류 전체보기 (191)
매일매일갱생 (83)
서버개발 (1)
임베디드개발 (12)
Programming (80)
Personal Projects (6)
유용한 프로그램 (0)
 TAGS
개발자 debugging C 영어 이메일 Brazil It DVB-T spam mail Algorithm Dshow Java C++ Windows Mobile6.0 DirectShow Debug warning isdbt 1seg 서태지 VC++ Error Case 미라지폰 project 군대 퇴사 음식 벨소리 변경 M480 MP3 티스토리초대 출장 Linux 알고리즘 ISDB-T 티스토리 초대장 DVB Wince5.0 english email English
 Calendar
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
 Recent Entries
 Recent Comments
 Recent Trackbacks
 Archive
 Link Site
zextor
괴짜 프로그래머의 일상사~@@
Gag & Peace, and more..
Kazakhstan Almaty.......
Min-A
Sadgarret
Steve Yoon's log
가슴 뛰는 삶을 살아라
오스틴 파워
GUI sin
melanie parker_Lady
제레미의 TV 2.0 이야기..
 Visitor Statistics
Total :
Today :
Yesterday :
rss