Address allignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcarthybn
    New Member
    • Jun 2009
    • 7

    #1

    Address allignment

    Hi ,
    Can someone please help me to know how can i check that a 32 bit address is alligned or invalid address ? Can it possible by C code?

    I am waiting for your reply.

    Thanks
    mcarthybn
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    You have to do something pretty tricky to get the compiler to put C variables at locations with improper alignment. On the other hand, if you're parsing a binary file instead of manipulating C variables then alignment might become an issue.

    Strategy 1: Avoid alignment problems by only reading the binary file a char at a time. Provide functions to assemble a wider data type from a char sequence. This approach works well with integer data but not so well with floating-point since you aren't supposed to know the implementation-dependent rules for how floating-point numbers are encoded. You have to worry about the endianness of the file.

    Strategy 2: Avoid alignment problems by using a text file instead of a binary file. The file expresses integer and floating-point values in ASCII.

    Strategy 3: Avoid alignment problems by only reading the binary file a char at a time. Provide a decoder function for each data type. The decoder functions copy sizeof(type) chars into the char-array portion of a union whose other member is the type in question. Return the value of that other union member. This strategy assumes that the file encoding matches the implementation-dependent encoding of your compiler.

    Strategy 4: Look up the implementation-dependent alignment rules for your processor. Write a verify-alignment function for each data type; input to these functions is a void* pointer. Each function casts the pointer to a big-enough integral value and tests the low-order bits to see if they match the required alignment of the data type. This tells you if there is an alignment error, but it doesn't help you read misaligned data.

    Comment

    Working...