Is cast between structs safe?

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

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*/
};
 
E

Eric Sosman

(e-mail address removed) wrote On 04/04/07 10:50,:
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).
 
D

David Thompson

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top