If a data structure will be used in both a 32bits binary and a 64bits
binary, e.g., an ioctl structure which is passed from a 32bit
application to the 64bit kernel, then we need to consider the code
alignment issue. Because:

  •   The structure size might be different for a same structure.
  •   The offset of each structure member might be different for a same
    structure.

The alignment can be decided by compiler options. The default compiler behavior will conform to the top rule: all structure members must start at its alignment addresses, and, the whole structure should start at the alignment address of its largest member.
For example, uint16_t will always start at even address, uint32_t will always align at 4 bytes boundary, etc. There are also some points as following:

  1.  

    The whole structure's size will be according to the biggest member's
    alignment. e.g., if the biggest member is uint32_t, then the whole
    sizeof(struct) will be divided exactly by 4bytes.

  2.  

    If there are "long", "pointer", or "long double" types in the
    structure
    members, then the alignment in 32bit and 64bit binaries will be
    different. Because they are different length. For example, if there is
    a pointer in a structure, then, in a 32bits binary, it aligns at 4
    bytes
    address; in a 64bits binary, it aligns at 8 bytes address.

  3.  

    The max alignment of 32bits binary is 32bits, The max alignment of
    64bits binary is 64bits. So, if there is a "long long" in a 32bits
    binary, it aligns at 4 bytes address; however, the same type aligns at
    8 bytes address in a 64bits binary.

  4.  

    "enum" type is considered as uint_t.

Code alignment feature can be disabled by setting specific compiler options, such as, gcc -fpack-struct, which will remove the code alignment feature, that is, there are no gaps between structure members.

And "pack" pragma can also do the same job, details refer to the related section of "C User's Guide". Note that the pragma might cause problems on different hardware platforms.

The following table is comparing the data types between 32bits and 64bits binaries.

C Type                      ILP32                LP64
 

char                            8                    8
  short                          16                   16
  int                              32                   32
  long                           32                   64
  long long                   64                   64
  float                           32                   32
  double                       64                   64
  long double               96                   128
  pointer                      32                   64


-----------------------------
An data structure Example:


    typedef enum list {

        a0 = 0,

        a1 = 1,

        a2 = 2,

    } list_t;


    struct align {

        uint16_t a;

        uint8_t  b;

        list_t    list;

        uint8_t    c;  

        uint8_t    d;  

    } sa;


offset_a=fffffd7fffdff954, offset_b=fffffd7fffdff956,
offset_list=fffffd7fffdff958, offset_c=fffffd7fffdff95c,
offset_d=fffffd7fffdff95d, sizeof_allign=12

Comments:

Your picture and sidebar obscures most of the text. Find someone unfortunate enough to be using IE and you'll see.

Posted by david mcdaniel on July 18, 2007 at 04:27 AM CST #

Yes.. I agree with david

Posted by DP on July 20, 2007 at 03:33 AM CST #

The problem is now fixed. Thanks for pointing out it.

Posted by Colin on August 03, 2007 at 05:51 PM CST #

this article is about memory alignment - not *code* alignment, as the title suggests.

Posted by dila on September 18, 2007 at 07:15 AM CST #

i mean it's about data alignment - "memory" is just as ambiguous, heh

Posted by dila on September 18, 2007 at 07:17 AM CST #

Yes, I think "data alignment" is better. I've modified the title of this article. Thanks for your comments. :-)

Posted by Colin on September 18, 2007 at 09:08 PM CST #

Hi
I have a query on following structure
struct sachin
{
char a;
short int b;
int c;
}

What to do with this structure that every field of this structure will aligned to 4 byte ? And sizeof () will come as 12.

Thanks
Sachin

Posted by Sachin Rastogi on June 04, 2008 at 09:42 PM CST #

Sachin,
I tested your structure, the sizeof() is 8. If your test result comes to 12, I guess is because of compiler options.

Posted by Colin Zou on July 13, 2008 at 04:21 PM CST #

Colin,
My query is "How to align every parameter of the above structure to 4 byte so that sizeof(sachin) will come to 12".

Sachin

Posted by Sachin on July 14, 2008 at 01:00 AM CST #

Move int c to the second element as following:
struct sachin
{
char a;
int c;
short int b;
}
Then the size will be 12.

Posted by Colin Zou on July 14, 2008 at 09:45 AM CST #

http://www.batterylaptoppower.com/hp/520.htm hp 520 battery ,
http://www.batterylaptoppower.com/hp/dv8000.htm hp dv8000 battery ,
http://www.batterylaptoppower.com/hp/dv8100.htm hp dv8100 battery ,

Posted by battery on November 03, 2008 at 04:44 PM CST #

I find the website laptop batteries,laptop AC adapters. All our products are brand new, with the excellent service from our laptop battery http://www.batterylaptoppower.com of customer service team.

the most convenient and cheap replacement battery online shop in uk. We specialize in http://www.batteryfast.com laptop batteries,laptop AC adapters. All our products are brand new, with the excellent service from our customer service team.

the most convenient and cheap replacement battery online shop in uk.
We specialize in laptop batteries http://www.batteryfast.co.uk ,laptop AC adapters. All of batteries are brand new, with the excellent service from our customer service team, you can feel free to purchase on laptop battery!

Here is cheap laptop ac adapter http://www.adapterlist.com online shop in uk. We specialize in http://www.batteryfast.com.au laptop AC adapters. All our products are brand new, with the excellent service from our customer service team.

Posted by Glynis Ayling on November 07, 2008 at 11:56 AM CST #

If you need (or want) a computer that’s easy to take along,you can see it from http://www.adapterlist.com/hp/dv8100.htm hp dv8100 battery , whcih offer the longlife and consistently reliable performance you need to get the most out of your notebook.

Posted by laptop bettery on March 04, 2009 at 09:36 AM CST #

The proposed battery maintenance practices. Other Battery Maintenance Practice is as follows: o In the 20 ° C and 25 ° C (68 ° F and 77 ° F), 30% to 50% of the electricity stored between the lithium-ion batteries. o Do not disassemble, squeezing, or puncture the battery; not short-circuit the battery of the external contacts; not use the battery As for the fire or water. http://www.bestlaptopbattery.co.uk/dell/inspiron-500m-series.htm o Do not let the battery long-term exposure at high temperatures. As for the battery long-term high-temperature (for example, on the car), the lithium-ion will accelerate degradation. o If the notebook computer is idle for more than two weeks (off and not plugged into AC power), please remove the battery. o If the laptop plugged into AC power continuously for more than two weeks (through a wall outlet or Masonry station), please remove the battery from the notebook computer. o If the notebook computer uses battery power to run high-end applications, use the highest rated capacity (Ah) battery types. ? o According to adjust battery usage patterns. Normal use, at least once every 3 months the battery adjustment; however, very few fully discharged battery should be adjusted once a month. ? o Do not let the children touch the battery. http://www.bestlaptopbattery.co.uk/dell/inspiron-510m-series.htm ? o came with your computer should be used only to provide the battery, Hewlett-Packard to provide replacement batteries or as an attachment compatible battery purchased.<br>
Battery saving tips Users can manually change the power consumption of notebook computers and save battery power. * Reduce the number of applications are running. Each application have to use memory, so will power, even if the application window will be minimized as well. Quit all applications no longer in use. * The contents of the screen brightness can be reduced to the lowest level of reading. Using the "Fn" and "F7" or "F8" key to adjust the brightness. * Unplug peripherals not in use.http://www.bestlaptopbattery.co.uk/dell/xps-m1730.htm External hard drives, CD-ROM, Zip drives, PC cards and other peripherals will consume battery power, if not the use of these peripherals as well. After you are finished using peripheral interrupt peripheral connections. * To reduce the speed of computer processors run faster, use the power the shorter the time. Reduce the processor speed extended battery life. Methods to reduce processor speed is different on different models, computer manuals provide detailed instructions. * Do not use wireless, please turn off the wireless switch button.http://www.bestlaptopbattery.co.uk/hp/338794-001.htm If the laptop has a wireless switch button, please press this button to turn off the wireless light.

Posted by bertina on September 15, 2009 at 04:05 PM CST #

Li-ion has the following advantages: 1) The single cell voltage of up to 3.6-3.8V: 2) than the energy larger than the present to achieve real energy 100-115Wh/kg and 240-253Wh / L (2 times the Nl-Cd, 1.5 times the Ni-MH), http://www.bestlaptopbattery.co.uk the future, as technological development, specific energy can be Up to 150Wh/kg and 400 Wh / L 3) The long cycle life in general can reach more than 500 times, or even 1000 times. For small electrical current discharge, the battery will double the lifespan of electrical appliances competitiveness. 4) The safety performance is good, pollution-free, no memory effect. As the predecessor of Li-ion batteries, due to the formation of lithium dendrite easy to short-circuit, reducing its application areas: http://www.bestlaptopbattery.co.uk/dell/inspiron-6000.htm Li-ion does not contain cadmium, lead, mercury, etc. elements of the environment pollution: Some processes (such as sintered) and Ni-Cd batteries there is a major drawback for the "memory effect", seriously hindered the use of batteries, but Li-ion does not exist at this problem. 5) The self-discharge of small Room temperature, full power Li-ion storage a month after the self-discharge rate of about 10%, much lower than Ni-Cd of 25-30%, Ni, MH 30-35%.<br>
Laptop battery are "high consumables" category, and its life is generally 1-2 years, relative to the machine price, the laptop battery is quite fairly inexpensive, so the average user will choose to buy after the battery end of life a new battery. However, the market there are a variety of laptop battery: original battery, secondary battery,http://www.bestlaptopbattery.co.uk/dell/latitude-d820.htm disassemble the battery, compatible battery ...... and so on, on these wide variety of laptop battery How are we going to buy? I write this article on the purchase of laptop battery hope that they intend to buy the battery's friends some reference and help.

Posted by Tom on October 14, 2009 at 11:59 AM CST #

Reader Reader is a kind of special equipment. There you can insert a memory card slot, there are port can be connected to the computer. To the

appropriate memory card slot, the port connected to the computer and install the necessary drivers, the computer put the memory card as a removable memory,
http://www.bestlaptopbattery.co.uk/dell/inspiron-9200.htm

which can read and write memory card reader. Readers are divided into dedicated and versatile card reader card reader, dedicated reader can only read a

specific memory card, while the composite multi-memory card reader can read and write. At present, there are already 8-in-one card reader. 32. DVD drive

Refers to the device to read DVD discs. DVD disc capacity of 4.7GB, equivalent to seven times the CD-ROM disc can store 133 minutes the film, including seven
http://www.bestlaptopbattery.co.uk/dell/inspiron-9300.htm

Dolby Digital surround tracks. DVD disc can be divided into: DVD-ROM, DVD-R (write at once), DVD-RAM (for multiple write) and DVD-RW (read and rewrite). The

current DVD drive to use more EIDE interfaces, can be the same as the CD-ROM drive connected to IDE1 or IDE2 port. 33. DVD recorder DVD burner is capable

of rewritable DVD disc to write and erase operations drive. The current DVD recording standards are not unified, there are three: DVD-RW standard, DVD + RW

specification, and is compatible with two kinds of specifications of DVD l burner.

Posted by bertina on October 24, 2009 at 07:00 PM CST #

If you pocket the silver is not adequate, then a high price to buy the Department of the camera, complete no need to care about metal shell, because the

current manufacturing processes for plastics technology is already very advanced, especially in the solidity and stability of the better than metal poor, and

sometimes feel even better than metal, so what kinds of material fact, the same shell, it is important is that this money out to value, do not just plan a

psychological comfort.<a href=http://www.bestlaptopbattery.co.uk/dell/inspiron-2200.htm>Dell inspiron 2200 battery</a> Digital Camera shopping easily accessible of the eight misunderstanding of the two: High-pixelPixel value of the current digital camera

is becoming increasingly high, a lot of about 3,000 products already reached 7 million, 8 million pixel level, in fact, people in the industry to understand

such a truth, that is, pixels at the same time increase the cost of the camera should also increase Why is the same for the 7 million pixels, some of the

camera, we sold 3,000 yuan, while others sell more than 6000 yuan cameras do? In fact, this is what we going to mention the high-pixel errors.Many consumers

like to use pixels to measure the level of photo quality, buy the camera when the first sentence is: "is the 8 million pixels it?", We must first negate this

view, because it can affect the image quality of digital cameras are many factors that The most important thing is the quality of the lens and CCD size,

because the pixels in the same circumstances, CCD is the size of the photo quality played a decisive factor, http://www.bestlaptopbattery.co.uk/dell/inspiron-630m.htm CCD small size, poor quality models, more noise,

so CCD is the more large as possible, but the price increase will follow. Another lens resolution and quality are also directly related to, or even have a

high number of pixels, image quality because the lens is still relatively poor reasons.It also reminded many consumers, many manufacturers like to play in

the pixel interpolation trick above, in order to compensate for the lack of hardware to use some software to increase the pixel values, although the effect

in the image have a certain improvement, but lower than the actual pixel value speaking, there are still big differences, many dealers interpolated pixel

value is to the actual pixel value with the concept of ambiguity to mislead consumers.http://www.bestlaptopbattery.co.uk/dell/inspiron-640m.htm

Posted by best laptop batteries on November 14, 2009 at 05:22 PM CST #

To see how the under-the-hood changes to the MacBook would affect performance, we ran the system through a series of 19 different tests involving the Finder and 12 third-party applications. We then compared the results to a number of reference systems.

Compared to the system it replaces, the new 2.26GHz MacBook was faster than the older 2.13GHz MacBook in every test we ran, though, in many cases, not by very much. Importing 150 snapshots into iPhoto on the 2.26GHz MacBook was just one second faster than on the older 2.13GHz MacBook. The new MacBook was able to push through about one frame per second more than the previous MacBook in our Call of Duty 4 test.http://www.bestlaptopbattery.co.uk/dell/precision-m65.htm

http://www.bestlaptopbattery.co.uk/dell/312-039.htm

In other tests the differences were more pronounced, like the nearly 13 percent improvement in our Photoshop CS4 test, 17 percent in Aperture tests, 10 percent in our iMovie import tests, and nearly 14 percent improvement in our iMovie export test.http://www.bestlaptopbattery.co.uk

Posted by zhupeilan1230 on November 16, 2009 at 03:38 PM CST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by colin