Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #20226

Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type?
Date 2012-05-02 12:36 -0400
Organization A noiseless patient Spider
Message-ID <4FA16280.6040703@verizon.net> (permalink)
References <37f36566-0b91-4ee4-9135-6890c7c8de7a@12g2000vba.googlegroups.com>

Show all headers | View raw


On 05/02/2012 12:06 PM, John Reye wrote:
> Assume identical common top struct members:
> 
> struct a {
>   int i1;
>   char c1;
>   short sa1[3];
> };
> 
> struct b {
>   int i2;
>   char c2;
>   short sa2[3];
> 
>   int differ_here;
> };
> 
> 
> struct a tmp;
> 
> 
> 
> Are the following 2 line always equivalent (as in: yielding the same
> lvalue) and allowed:
> 
> tmp.c1
> ((struct b*)&tmp)->c1

No, the behavior is undefined. It's not because the members might be a
different locations in the two structs; that's possible but unlikely.
The real reason is the anti-aliasing rules (6.5p7). Because those rules
make the behavior of such code undefined, the implementation is not
obligated to consider the possibility that an lvalue referring to a
"struct a" object refers to the same object as one that refers to a
"struct b" object. As a result, when implementing code such as the
following:

	tmp.c1 = 1;
	printf("%d\n", ((struct b*)&tmp)->c1);

An implementation is not required to notice that the printf() is
referring to the same object as the assignment statement. As a result,
it could, for instance, defer the writing the new value to tmp.c1 until
after executing the printf() call. That's pretty unlikely in this simple
case, but gets more likely in more complicated code when aggressive
optimization is turned on.

There's a special exception that allows you to access the "common
initial sequence" of any two struct types, using either struct type,
when they are both members of the same union (6.5.2.3p6):

	union ab
	{
		struct a one;
		struct b two;
	} pair;
	pair.one.c1 = 1
	printf("%d\n", pair.two.c1);

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-02 09:06 -0700
  Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? "In a very dark China Blue, China Blue condition." <chine.bleu@yahoo.com> - 2012-05-02 09:33 -0700
  Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-02 12:36 -0400
    Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-02 10:04 -0700
      Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-02 13:23 -0400
    Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 09:20 -0700
      Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 09:25 -0700
        Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 10:16 -0700
        Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 11:05 -0700
        Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-03 15:21 -0400
      Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 09:46 -0700
      Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Nobody <nobody@nowhere.com> - 2012-05-03 19:14 +0100
        Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 11:45 -0700
          Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Jens Gustedt <jens.gustedt@loria.fr> - 2012-05-03 21:05 +0200
            Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 12:14 -0700
            Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-03 15:16 -0400
              Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Lanarcam <lanarcam1@yahoo.fr> - 2012-05-03 21:56 +0200
                Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-03 16:12 -0400
                Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 13:45 -0700
                Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Nobody <nobody@nowhere.com> - 2012-05-04 01:34 +0100
              Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Jens Gustedt <jens.gustedt@loria.fr> - 2012-05-03 22:32 +0200
            Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 12:10 -0700
              Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Keith Thompson <kst-u@mib.org> - 2012-05-03 16:14 -0700
            Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-07 17:24 -0700
          Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-03 15:19 -0400
            Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? John Reye <jononanon@googlemail.com> - 2012-05-03 12:25 -0700
      Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-03 17:44 -0400
  Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Barry Schwarz <schwarzb@dqel.com> - 2012-05-02 12:35 -0700
    Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-05-02 14:40 -0700
    Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? James Kuyper <jameskuyper@verizon.net> - 2012-05-02 17:51 -0400
    Re: Can *common* struct-members of 2 different struct-types, that are the same for the first common members, be accessed via pointer cast to either struct-type? Tim Rentsch <txr@alumni.caltech.edu> - 2012-05-07 17:16 -0700

csiph-web