Byte alignment?

Discussion in 'C Programming' started by Jim Ward, Aug 15, 2005.

  1. Jim Ward

    Jim Ward Guest

    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?
     
    Jim Ward, Aug 15, 2005
    #1
    1. Advertising

  2. Jim Ward

    Michael Mair Guest

    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.
     
    Michael Mair, Aug 15, 2005
    #2
    1. Advertising

  3. Jim Ward

    Randy Howard Guest

    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)
     
    Randy Howard, Aug 15, 2005
    #3
  4. Jim Ward

    rick Guest

    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
     
    rick, Aug 15, 2005
    #4
  5. 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
     
    Igmar Palsenberg, Aug 16, 2005
    #5
  6. Jim Ward

    Jim Ward Guest

    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
     
    Jim Ward, Aug 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rathnow

    JNI with 1 byte alignment crashes JVM

    Dave Rathnow, Nov 7, 2003, in forum: Java
    Replies:
    2
    Views:
    1,785
    Gordon Beaton
    Nov 7, 2003
  2. Ares Lagae

    Byte alignment in union

    Ares Lagae, Aug 25, 2003, in forum: C++
    Replies:
    21
    Views:
    4,199
    Michiel Salters
    Sep 1, 2003
  3. Marco

    byte alignment

    Marco, Jan 21, 2005, in forum: C++
    Replies:
    4
    Views:
    504
    EventHelix.com
    Jan 22, 2005
  4. Shashi

    Byte Alignment

    Shashi, Nov 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    572
    Thomas Matthews
    Nov 10, 2003
  5. Replies:
    1
    Views:
    392
    Lawrence Kirby
    Jul 6, 2005
Loading...

Share This Page