Byte alignment?

J

Jim Ward

After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);

int main(int argc, char* argv[])
{
struct sockaddr_in sockaddr;
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?
 
M

Michael Mair

Jim said:
After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);

int main(int argc, char* argv[])
{
struct sockaddr_in sockaddr;
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?

Guess: You had bad luck all the years before -- it worked even
though it did not have to. Now it's biting you. With sockaddr
declared on file scope, you get unlucky again.
Maybe struct sockaddr has stricter alignment requirements than
struct sockaddr_in -- and now, for the first time, it goes wrong.

If you want people to be able to help you, give at least the
definitions of the two struct types and the source of accept().
If you try to create a compiling minimal example, you maybe
find out by yourself what the problem is.


Good luck
Michael
 
R

Randy Howard

Jim Ward wrote
(in article said:
After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);

Then you would be best served to ask about it in
comp.unix.solaris probably instead of here.
 
R

rick

Jim Ward wrote:
but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?

I just read about this. Allow me a quote from the K&R book.

"In the absence of explicit initialization, external and static
variables are guaranteed to be initialized to zero; automatic and
register variables have undefined (i.e., garbage) initial values." -
K&R, section 4.9.

hth.
~rick
 
I

Igmar Palsenberg

Jim said:
but if I declare sockaddr as a global, everything works:

Globals are 0 initialized, locals are not.
struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?

It hasn't. You just got lucky :) The most common case is that addr_size
isn't initialized to sizeof(sockaddr_in) *before* calling accept().



Igmar
 
J

Jim Ward

Igmar Palsenberg said:
It hasn't. You just got lucky :) The most common case is that addr_size
isn't initialized to sizeof(sockaddr_in) *before* calling accept().

Yes, that was the problem! The manpage says you have to do this, but
my 1989 O'Reilly copy of "Using C on the Unix System" leaves this step
out. I need to get a new copy!

Thanks to all who answered!

Jim Ward
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top