Portable struct initialization

E

Everton

The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

/* NetBSD - /usr/include/netinet6/in6.h */
struct in6_addr {
union {
__uint8_t __u6_addr8[16];
__uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
} __u6_addr; /* 128-bit IP6 address */
};

/* Linux - /usr/include/netinet/in.h */
struct in6_addr
{
union
{
uint8_t u6_addr8[16];
uint16_t u6_addr16[8];
uint32_t u6_addr32[4];
} in6_u;
};

/* Solaris - /usr/include/netinet/in.h */
struct in6_addr {
union {
uint8_t _S6_u8[16]; /* IPv6 address */
uint32_t _S6_u32[4]; /* IPv6 address */
uint32_t __S6_align; /* Align on 32 bit
boundary */
} _S6_un;
};

Is there a way to build a portable static initializer
for struct in6_addr, other than selecting among several
compile-time codes by using precompiler directives?
Or else, would it be safer to use a non-static initialization?
Please advise.
 
C

Chris Dollin

Everton said:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

If the variable is non-local then

struct in6_addr myVariable;

If the variable is local, in which case you have to use a
dynamic initialisation, then:

static const struct in6_addr zero;

... inside a function ...
struct in6_addr myLocal = zero;
 
F

Flash Gordon

Everton said:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:

/* NetBSD - /usr/include/netinet6/in6.h */
struct in6_addr {
union {
__uint8_t __u6_addr8[16];
__uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
} __u6_addr; /* 128-bit IP6 address */
};

/* Linux - /usr/include/netinet/in.h */
struct in6_addr
{
union
{
uint8_t u6_addr8[16];
uint16_t u6_addr16[8];
uint32_t u6_addr32[4];
} in6_u;
};

/* Solaris - /usr/include/netinet/in.h */
struct in6_addr {
union {
uint8_t _S6_u8[16]; /* IPv6 address */
uint32_t _S6_u32[4]; /* IPv6 address */
uint32_t __S6_align; /* Align on 32 bit
boundary */
} _S6_un;
};

Is there a way to build a portable static initializer
for struct in6_addr, other than selecting among several
compile-time codes by using precompiler directives?
Or else, would it be safer to use a non-static initialization?
Please advise.

If you want them initialised to 0 then it is easy.
struct in6_addr fred = { 0 };
Alternatively, since this is obviously an instance where all bits 0 is
going to be valid (knowledge based on stuff outside the C standard), you
can use:
memset(fred, 0, sizeof fred);

However, this does not help you with accessing the individual elements.
 
E

Everton

I'm trying to figure out whether it would be safe (portable)
to define a static initialization like the example below.
Could alternate definitions of in6_addr's members
lead to unexpected behavior on some platform,
yielding an address distinct from the one expected?

$ more init.c
/* init.c */
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>

struct in6_addr addr = { 1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16 };

int main() {
unsigned char *i = (unsigned char *) &addr;
unsigned char *past_end = i + 16;
for (; i < past_end; ++i) printf("%d ", *i);
printf("\n");
exit(0);
}
$ gcc -Wall -o init init.c
init.c:7: warning: missing braces around initializer
init.c:7: warning: (near initialization for `addr.in6_u')
$ ./init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Everton said:
The task at hand is to initialize a variable of
type "struct in6_addr" in a portable way. For instance:
Isn't it more interresting HOW you want to initialize them ?
Quite so often you want to place a converted textual ip address in
there, and there are posix functions for doing so. Use those.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top