Connecting Tech Pros Worldwide Forums | Help | Site Map

Is cast between structs safe?

lovecreatesbea...@gmail.com
Guest
 
Posts: n/a
#1: Apr 4 '07
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:

struct sockaddr add1, *add2; /*see struct declarations below*/
struct sockaddr_in add_in;
struct sockaddr_in6 add_in6;
void *p;

add1 = (struct sockaddr) add_in; /*LINE 6*/

p = &add_in6;
add2 = p; /*LINE 9*/
/*add2 = (struct sockaddr*) (p);*/

/******************************************/

struct sockaddr{
sa_family_t sa_family; /*address family*/
char sa_data[14]; /*variable-length address*/
};

struct sockaddr_in{
sa_family_t sin_family; /*address family*/
in_port sin_port; /*port number*/
struct in_addr {
in_addr_t s_addr;
} sin_addr; /*IPv4 address*/
};

struct sockaddr_in6{
sa_family_t sin6_family; /*address family*/
in_port sin6_port; /*port number*/
uint32_t sin6_flowinfo; /*traffic class and flow
info*/
struct in6_addr {
uint8_t s_addr[16];
} sin6_addr; /*IPv6 address*/
uint32_t sin6_scope_id; /*set of interface for
scope*/
};


Eric Sosman
Guest
 
Posts: n/a
#2: Apr 4 '07

re: Is cast between structs safe?


lovecreatesbea...@gmail.com wrote On 04/04/07 10:50,:
Quote:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:
>
struct sockaddr add1, *add2; /*see struct declarations below*/
struct sockaddr_in add_in;
struct sockaddr_in6 add_in6;
void *p;
>
add1 = (struct sockaddr) add_in; /*LINE 6*/
[...]
Are you sure you have reproduced the code accurately?
A conforming C compiler is required to emit a diagnostic
for your "LINE 6," which I suspect is missing a few
asterisks and ampersands. Please try again, or confirm
that the code you've posted is accurate (in which case,
it isn't C).

--
Eric.Sosman@sun.com
David Thompson
Guest
 
Posts: n/a
#3: Apr 15 '07

re: Is cast between structs safe?


On 4 Apr 2007 07:50:38 -0700, "lovecreatesbea...@gmail.com"
<lovecreatesbeauty@gmail.comwrote:
Quote:
I read `Advanced Programming in the UNIX Environment, 2nd ed.'. In
section 16.3.2, the book says that variables type struct sockaddr_in;
and/or struct sockaddr_in6; will be cast explicitly to variables type
struct sockaddr; Is this safe? Or will these casts between different
structs result in object slices? see casts at line 6 & 9 for example:
>
You can't cast struct types in C; if you could, slicing might indeed
be a problem. What you do with the various sockaddr_ structures is
cast _pointers to them_, and where relevant also keep track of the
length explicitly e.g. in the third argument to accept().

Closed Thread