Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Byte alignment?

Reply
Thread Tools

Byte alignment?

 
 
Jim Ward
Guest
Posts: n/a
 
      08-15-2005
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?
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      08-15-2005
Jim Ward wrote:
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Randy Howard
Guest
Posts: n/a
 
      08-15-2005
Jim Ward wrote
(in article <>):

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

--
Randy Howard (2reply remove FOOBAR)

 
Reply With Quote
 
rick
Guest
Posts: n/a
 
      08-15-2005

Jim Ward wrote:
<snip>
> but if I declare sockaddr as a global, everything works:
>
> struct sockaddr_in sockaddr;
>
> int main(int argc, char* argv[])

<snip>
> 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

 
Reply With Quote
 
Igmar Palsenberg
Guest
Posts: n/a
 
      08-16-2005
Jim Ward wrote:
> 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
 
Reply With Quote
 
Jim Ward
Guest
Posts: n/a
 
      08-22-2005
Igmar Palsenberg <> wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
byte[] > string > byte[] problem Peter Java 3 08-05-2004 10:55 AM
connection 1: transfer chunk 1 (byte 0 to byte 1024) Jean-Daniel Gamache Java 0 07-14-2004 03:57 AM
Single byte addressable, multiple byte readout. Andreas VHDL 1 05-04-2004 01:49 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57