Thread: Alginment issue.

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Alginment issue.

    gcc 4.1.1 (cross compiler)
    gcc 3.3.2 (cross compiler)
    Both are for the ARM.

    The structure:
    Code:
    struct something {
            uint8_t num;
            uint16_t val;
            uint16_t time;
    }
    the following code is compiled with BOTH compilers:
    Code:
    int main(void)
    {
            printf("Size of struct something = %i.\n", sizeof(struct something));
            return 0;
    }
    the results:
    gcc 4.1.1: 6
    gcc 3.3.2: 8

    So, without using __attribute__((aligned (2),packed)), is there any other option I have?

    [EDIT]Fixed the compiler versions. . . [/edit]
    Last edited by Kennedy; 06-30-2009 at 10:50 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, if you require a specific alignment, then you're stuck using compiler specific extensions to ensure the required alignment.

    The other option is to change the design such that a specific alignment isn't required.

    gg

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    What Codeplug said, but I just wanted to add that most compilers, AFAIK, support this syntax:

    Code:
    #pragma pack(push, 2)
    //...declarations
    #pragma pack(pop)
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hmmph. Well, as it turned out, the problem was not an alignment issue. The structure above was copied in memory to a "stream" where I added an identifier to the first part of it. That identifier was 1 byte long. This caused the user space application (3.3.2) to attempt to access the data on the word alignment. Since the first byte of the structure was on an odd memory address, the compiler accessed the memory in "grows down" fashion. . . the fix was to add in an additional byte to the front of the stream to ensure that the structure started on a word.

    That's just freaky.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, I would call that alignment of the entire structure. You still have to ensure consistency of the byte-stream representation of the structure - meaning structure size and member alignment should be the same for all compilers.

    When reading from a byte stream, you could just memcpy() the bytes of the structure from the stream into a local stack variable - instead of sprinkling bytes into the stream so that whole structure alignment is correct.

    gg

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Excellent suggestion. Hadn't thought of that. The only problem, however, is that we only control the kernel space memory usage, so I'm still outta luck. The issue is that I form up the data in the kernel and when I send it up I cannot govern what the user does with it. (I don't control both ends of the pipe.)

    Like I said, though, excellent suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. re-entrancy pattern issue setbacks
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 04-12-2008, 02:23 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. directsound issue
    By valis in forum Tech Board
    Replies: 0
    Last Post: 06-25-2006, 09:28 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM