Keil Logo

GCC: ALIGNMENT PROBLEM WITH VARIABLES WITH GNU


Information in this article applies to:

  • GNU C Compiler for ARM Version 3.22

QUESTION

I have declared just a few variables in my application, but the data space seems to be quickly used up. When I define the following variables, it appears that there are big gaps in the memory layout:

  int   ival;
  short sval;
  long  lval;

What is the reason for that?

ANSWER

The GNU compiler has an alignment problem with uninitialized data. There are two possible work-arounds:

  • You may use variable initialization which avoids such gaps. For example:
      int   ival = 0;
      short sval = 0;
      long  lval = 0;
    
  • An alternative is the GNU compiler directive -fno-common (enter under Options - CC - Misc Contorls: -fno-common). Using -fno-common, variables are placed into the .bbs section where alignment problems do not exist. However you may get side effects with libraries, since the library files must be translated consistently.

    To prevent the linker error Not enough room for program headers, try linking with -N, you need to add under Options - Linker - Misc Controls: -N.

Note: There have been many different variants of the GCC compiler for ARM devices. Modern versions of Keil still support the GCC compiler, but now uses the GNU Arm Embedded Toolchain. To use this toolchain with Keil, see GCC: Using a GNU GCC Compiler Toolchain in the IDE.

SEE ALSO

Last Reviewed: Thursday, August 9, 2018


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.