Thread: How to fix misaligned assignment statements in the source code?

  1. #1
    Registered User
    Join Date
    May 2006

    How to fix misaligned assignment statements in the source code?

    I'm not a C/C++ programmer, but I'm familiar with this programm language.

    The thing is that I'm trying to compile and run DDS (Darwin Streaming Server) on "Gentoo on Sparc" architecture.

    Although I've managed to compile DSS under my arch. I'm constatntly get "Bus error" while trying to connect to DSS with QT player.

    I've read that:

    "The "bus error" signal usually means a data access was not properly aligned. It is always due to an error in the program.

    One major cause is accessing data via a pointer of a different type that has different alignment requirements. Example:

    short s[2]; // 2-byte alignment
    int* p = (int*)s; // pretend the 2 shorts are one int int i = *p; // bus error is likely
    On SPARC, an int must be aligned on a 4-byte boundary, but a short only needs 2-byte alignment. The attempted load fails with a bus error if array s was not 4-byte aligned.

    Here is the example of resolution for this kind of problem on another project (a snap from the .diff):

    @@ -499,8 +499,11 @@
             conn->request_len -= req->len;
             if (conn->request_len == 0)
    +#if defined (__i386__)
     req = (void *) req + req->len;
    +memmove(&conn->request, (void *)req + req->len, conn->request_len); 
         if (conn->request_len > 0 && req != &conn->request)
    So I thought there may be some way to overcome these alignment requirements by may be replacing (with some search and replace func.) int and short definitions with the proper ones?

    As I've said I'm not a C/C++ programmer so I hope you could give me some good advices to resolve this problem.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  3. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM