public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* strict aliasing and pointer to struct
@ 2005-10-11  9:37 nrmj
  2005-10-11 20:03 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: nrmj @ 2005-10-11  9:37 UTC (permalink / raw)
  To: gcc-help

I have been googling a lot on the web about alias
analysis and strict aliasing, but haven't found much
about what is allowed or not when working with
structures.

1) If I understand the C standard correctly, the
compiler is free to assume that two pointers pointing
to different types don't alias.

   If two struct have different tag name, like:
    struct s1 {int i;};
    struct s2 {int i;};
    struct s1 *p1;
    struct s2 *p2;

   p1 and p2 are considered by the compiler to point
to different locations and don't alias, because p1
points to type "struct s1" and p2 points to type
"struct s2" which are different types, even if they
have the same members, right ?


2) Is it safe to cast pointer to struct B to pointer
to struct A, in order to fake inheritance, like in
this code sniplet ?
   ( and if it is not safe, how can I fake inheritance
? )

-------------------- myfile.c ----------------
#include <stdio.h>
#include <stdlib.h>

typedef enum { RED, BLUE, GREEN } Color;

struct Point { int x;
               int y;
             };

struct Color_Point { int   x;
                     int   y;
                     Color color;
                   };

struct Color_Point2{ struct Point point;
                     Color        color;
                   };

int main(int argc, char* argv[])
{

struct Point* p;

struct Color_Point* my_color_point =
malloc(sizeof(struct Color_Point));
my_color_point->x = 10;
my_color_point->y = 20;
my_color_point->color = GREEN;

p = (struct Point*)my_color_point;     // is it
allowed by C standard ?

printf("x:%d, y:%d\n", p->x, p->y);


struct Color_Point2* my_color_point2 =
malloc(sizeof(struct Color_Point2));
my_color_point2->point.x = 100;
my_color_point2->point.y = 200;
my_color_point2->color = RED;

p = (struct Point*)my_color_point2;   // is it allowed
by C standard ?

printf("x:%d, y:%d\n", p->x, p->y);

return 0;
}

Is this example also a case of what is called
"type-punning" ?


3) When programming with socket, the following cast to
(struct sockaddr *) is very common.
   But is it really safe ?

   struct sockaddr_in my_addr;
   ...
   bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr));


Best regards




	

	
		
___________________________________________________________________________ 
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger 
Téléchargez cette version sur http://fr.messenger.yahoo.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: strict aliasing and pointer to struct
  2005-10-11  9:37 strict aliasing and pointer to struct nrmj
@ 2005-10-11 20:03 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2005-10-11 20:03 UTC (permalink / raw)
  To: nrmj; +Cc: gcc-help

nrmj <maa1666@yahoo.fr> writes:

> I have been googling a lot on the web about alias
> analysis and strict aliasing, but haven't found much
> about what is allowed or not when working with
> structures.

I don't think these questions have anything to do with gcc.  I
recommend that you ask on some place like comp.std.c.

> 1) If I understand the C standard correctly, the
> compiler is free to assume that two pointers pointing
> to different types don't alias.
> 
>    If two struct have different tag name, like:
>     struct s1 {int i;};
>     struct s2 {int i;};
>     struct s1 *p1;
>     struct s2 *p2;
> 
>    p1 and p2 are considered by the compiler to point
> to different locations and don't alias, because p1
> points to type "struct s1" and p2 points to type
> "struct s2" which are different types, even if they
> have the same members, right ?

You have to watch out for the exception in C99 6.5.2.3 if there is a
visible union declaration which contains both structs.  But otherwise,
yes, they are different types.

> 2) Is it safe to cast pointer to struct B to pointer
> to struct A, in order to fake inheritance, like in
> this code sniplet ?

No, that is not safe.

But it is safe to do

  struct Color_Point { struct Point p; Color color; };
  p = &my_color_point.p;

> 3) When programming with socket, the following cast to
> (struct sockaddr *) is very common.
>    But is it really safe ?
> 
>    struct sockaddr_in my_addr;
>    ...
>    bind(sockfd, (struct sockaddr *)&my_addr,
> sizeof(struct sockaddr));

That is safe because the code does not dereference the resulting
pointer.

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-10-11 20:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-11  9:37 strict aliasing and pointer to struct nrmj
2005-10-11 20:03 ` Ian Lance Taylor

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).