Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Andreas <nospam@invalid.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Aliasing and subtyping structs |
| Date | 2016-11-08 00:24 +0100 |
| Organization | news.babsi.de for www.open-news-network.org |
| Message-ID | <87bmxqeuv4.fsf@starless.invalid> (permalink) |
| References | <87oa1zgzw0.fsf@starless.ames.family> <87k2cngz0i.fsf@starless.ames.family> <87lgx3md9o.fsf@bsb.me.uk> <87pome2rqn.fsf@starless.invalid> <87a8dhlw6g.fsf@bsb.me.uk> |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Do you have an example of something that someone has said is a problem
> that is worrying you?
I did some experiments (inspired by
http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html),
sorry for the somewhat lengthy code following:
<test_alias.c>
#include <stddef.h> /* size_t */
#include <stdint.h> /* fixed-width integers */
#include <string.h> /* memcpy */
struct foo
{
uint16_t a;
};
struct bar
{
uint16_t b;
};
struct base
{
uint32_t b;
};
struct derived
{
struct base base;
uint32_t d;
};
struct more_derived
{
struct derived base;
uint32_t m;
};
uint32_t foobar1 (uint32_t v)
{
uint16_t* const sp = (uint16_t*) &v;
uint16_t hi = sp[0];
uint16_t lo = sp[1];
sp[1] = hi;
sp[0] = lo;
return v;
}
uint32_t foobar2 (uint32_t v)
{
uint8_t tmp[2] = {};
uint8_t* const b = (uint8_t*) &v;
(void) memcpy (tmp, b + 2, 2);
(void) memcpy (b + 2, b, 2);
(void) memcpy (b, tmp, 2);
return v;
}
void foobar3 (uint32_t* four, uint16_t* two, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
four[x] = ((uint32_t) *two) + x;
}
}
void foobar4 (uint32_t* four, int32_t* ofour, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
four[x] = ((uint32_t) *ofour) + x;
}
}
void foobar5 (struct foo* f, struct bar* b, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
f[x].a = b->b + x;
}
}
void foobar6 (struct derived* d, struct base* b, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
*((uint32_t*) &d[x]) = b->b + x;
}
}
void foobar7 (struct more_derived* m, struct base* b, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
*((uint32_t*) &m[x]) = b->b + x;
}
}
void foobar8 (struct base* b, uint32_t* v, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
b[x].b = *v + x;
}
}
void foobar9 (struct base* b, uint16_t* v, size_t len)
{
size_t x;
for (x = 0; x < len; ++x)
{
b[x].b = *v + x;
}
}
</test_alias.c>
If you compile that code with (GCC 4.9 and CLANG 3.5):
gcc -g -fverbose-asm -Wall -m32 -fstrict-aliasing -O3 -S -o test_alias_32_O3.s test_alias.c
clang -g -fverbose-asm -Wall -m32 -fstrict-aliasing -O3 -S -o test_alias_clang_32_O3.s test_alias.c
than this results in the following:
- foobar1: v and b do not alias, although they probably could according
to c99 6.5.7 (I guess strictly speaking it could just return v without
doing anything else)
- foobar2: v and b do not alias and must not acc. to 6.5.7; it swaps
words portably
Interestingly enough both compilers emit identical code for foobar1 and
foobar2 (NB: GCC emits good code, two fetches and three stores, but the
code produced by CLANG is impressive; I guess one cannot write faster
assembler code complying to x86 standard calling convention even
manually)
- foobar3: four and two alias; if they somewhere refer to the same
stored value, results may not be, what you expect (nasal daemons etc.)
- foobar4: four and ofour do not alias as expected acc. to 6.5.7
- foobar5: f and b alias as expected acc. to 6.5.7
- foobar6: d and b do not alias as expected acc. to 6.5.7
- foobar7: m and b do not alias as expected acc. to 6.5.7
- foobar8: b and v do not alias as expected acc. to 6.5.7; note that
struct base contains a member of type uint32_t
- foobar9: b and v alias as expected acc. to 6.5.7; no uint16_t in
struct base
So my initial doubt about casting from base to derived and vice versa
not being reflexive in the standard was based on fallacy. I guess it
does not matter how two pointers get their values, but the aliasing rule
in the standard just says that the compiler needs to assume that a
pointer to base and a pointer to derived might refer the same stored
value.
Thanks for your support,
Andreas
--
What do you mean? An African or a European swallow?
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-01 13:15 +0100
Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-01 13:34 +0100
Re: Aliasing and subtyping structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 15:27 +0000
Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-02 09:47 +0100
Re: Aliasing and subtyping structs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-02 15:47 +0000
Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-08 00:24 +0100
Re: Aliasing and subtyping structs supercat@casperkitty.com - 2016-11-07 21:42 -0800
Re: Aliasing and subtyping structs <william@wilbur.25thandClement.com> - 2016-11-03 18:53 -0700
Re: Aliasing and subtyping structs supercat@casperkitty.com - 2016-11-04 09:56 -0700
Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-08 21:53 +0100
Re: Aliasing and subtyping structs James Kuyper <jameskuyper@verizon.net> - 2016-11-01 11:41 -0400
Re: Aliasing and subtyping structs Andreas <nospam@invalid.invalid> - 2016-11-02 09:55 +0100
"Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-21 16:59 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-21 07:32 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-21 11:15 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-21 08:35 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-22 11:42 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 06:50 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-22 16:07 +0000
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 12:02 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-22 23:36 +0000
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 16:24 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Richard Damon <Richard@Damon-Family.org> - 2016-11-22 22:21 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-23 08:31 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] me <crisdunbar@gmail.com> - 2016-11-23 09:01 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-23 12:05 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-11-22 23:23 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-22 16:02 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-22 14:13 -0800
Re: container_of() macro Noob <root@127.0.0.1> - 2016-11-23 01:10 +0100
Re: container_of() macro <william@wilbur.25thandClement.com> - 2016-11-22 20:10 -0800
Re: container_of() macro "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 15:10 -0800
Re: container_of() macro David Brown <david.brown@hesbynett.no> - 2016-11-23 11:31 +0100
Re: container_of() macro Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-23 14:07 +0300
Re: container_of() macro supercat@casperkitty.com - 2016-11-23 08:41 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-11-30 00:14 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-11-29 17:23 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-04 18:48 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-04 12:17 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-04 22:15 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-04 15:06 -0500
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@gmail.com> - 2016-12-12 22:31 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 13:39 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] luser droog <luser.droog@gmail.com> - 2016-11-23 15:10 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-23 15:13 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] luser droog <luser.droog@gmail.com> - 2016-11-23 18:29 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Anton Shepelev <anton.txt@g{oogle}mail.com> - 2016-11-24 11:41 +0300
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-24 03:29 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-24 17:37 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-25 09:55 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-25 15:22 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-11-28 18:18 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-29 04:48 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-29 07:28 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-11-30 08:26 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] supercat@casperkitty.com - 2016-11-30 09:13 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] "Chris M. Thomasson" <invalid@invalid.invalid> - 2016-12-12 16:44 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Melzzzzz <mel@zzzzz.com> - 2016-12-13 02:18 +0100
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 09:47 -0800
Re: "Object-oriented programming in C" [was: Re: Aliasing and subtyping structs] Thiago Adams <thiago.adams@gmail.com> - 2016-12-14 10:14 -0800
csiph-web