* ignore that strict-aliasing warning ?
@ 2007-10-18 10:22 Michael Haubenwallner
2007-10-19 2:54 ` Andrew Haley
0 siblings, 1 reply; 2+ messages in thread
From: Michael Haubenwallner @ 2007-10-18 10:22 UTC (permalink / raw)
To: gcc-help
Hi,
please help me understand these strict-aliasing warnings:
$ cat test.c
/* 1*/ extern void allocate(int size, void** p);
/* 2*/
/* 3*/ typedef struct X {
/* 4*/ char A;
/* 5*/ } X;
/* 6*/
/* 7*/ X* test1(void)
/* 8*/ {
/* 9*/ X *x;
/*10*/ allocate((int)sizeof(X), (void**)&x); /* current code */
/*11*/ allocate((int)sizeof(X), &x); /* as older gcc warned here already */
/*12*/ return x;
/*13*/ }
/*14*/
/*15*/ X* test2(void)
/*16*/ {
/*17*/ union {
/*18*/ X *x;
/*19*/ void* v;
/*20*/ } u;
/*21*/ allocate((int)sizeof(X), &u.v);
/*22*/ allocate((int)sizeof(X), (void**)&u);
/*23*/ return u.x;
/*24*/ }
$ gcc -Wall -O2 -c test.c -Wstrict-aliasing=2
test.c: In function 'test1':
test.c:10: warning: dereferencing type-punned pointer will break strict-aliasing rules
test.c:11: warning: passing argument 2 of 'allocate' from incompatible pointer type
test.c: In function 'test2':
test.c:22: warning: dereferencing type-punned pointer might break strict-aliasing rules
Strange is that line 10 "will", but line 22 "might" break
strict-aliasing rules.
Do I really need the union here like in test2() and line 21 ?
Or is there another "correct" way to get rid of warning in line 10 ?
Thanks!
/haubi/
--
17.-19. Oktober 2007
Salomon Automation am 24. Deutschen Logistik-Kongress der BVL, Berlin; Stand W/09
14. November 2007
Salomon Automation an der LogIT Retail im Leopoldsmuseum in Wien
Salomon Automation GmbH - Friesachstrasse 15 - A-8114 Friesach bei Graz
Sitz der Gesellschaft: Friesach bei Graz
UID-NR:ATU28654300 - Firmenbuchnummer: 49324 K
Firmenbuchgericht: Landesgericht für Zivilrechtssachen Graz
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: ignore that strict-aliasing warning ?
2007-10-18 10:22 ignore that strict-aliasing warning ? Michael Haubenwallner
@ 2007-10-19 2:54 ` Andrew Haley
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Haley @ 2007-10-19 2:54 UTC (permalink / raw)
To: Michael Haubenwallner; +Cc: gcc-help
Michael Haubenwallner writes:
> $ cat test.c
> /* 1*/ extern void allocate(int size, void** p);
> /* 2*/
> /* 3*/ typedef struct X {
> /* 4*/ char A;
> /* 5*/ } X;
> /* 6*/
> /* 7*/ X* test1(void)
> /* 8*/ {
> /* 9*/ X *x;
> /*10*/ allocate((int)sizeof(X), (void**)&x); /* current code */
> /*11*/ allocate((int)sizeof(X), &x); /* as older gcc warned here already */
> /*12*/ return x;
> /*13*/ }
> /*14*/
> /*15*/ X* test2(void)
> /*16*/ {
> /*17*/ union {
> /*18*/ X *x;
> /*19*/ void* v;
> /*20*/ } u;
> /*21*/ allocate((int)sizeof(X), &u.v);
> /*22*/ allocate((int)sizeof(X), (void**)&u);
> /*23*/ return u.x;
> /*24*/ }
> $ gcc -Wall -O2 -c test.c -Wstrict-aliasing=2
> test.c: In function 'test1':
> test.c:10: warning: dereferencing type-punned pointer will break strict-aliasing rules
> test.c:11: warning: passing argument 2 of 'allocate' from incompatible pointer type
> test.c: In function 'test2':
> test.c:22: warning: dereferencing type-punned pointer might break strict-aliasing rules
>
> Strange is that line 10 "will", but line 22 "might" break
> strict-aliasing rules.
I don't see what's strange about it. Assigning to u.v and then
converting the address of u to a void** is perfectly legal C. It's a
pointless thing to do, though, and rather than this you might as well
do Line 21.
> Do I really need the union here like in test2() and line 21 ?
There's no way for us to tell.
> Or is there another "correct" way to get rid of warning in line 10 ?
If you could tell us what you're really trying to do we might be able
to help. The code in Line 10 makes no sense at all: it has no
meaning, and can't possibly do anything useful.
The sensible way to do what you need is simply:
void *p;
allocate((int)sizeof(X), &p);
X *x = (x*)p;
Andrew.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-10-18 10:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-18 10:22 ignore that strict-aliasing warning ? Michael Haubenwallner
2007-10-19 2:54 ` Andrew Haley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).