最近总是遇到关于C语言的一些对于内存的处理,现总结如下:
(1)#pragma pack(4)
struct One{
double d;
char c;
int i;
}
struct Two{
char c;
double d;
int i;
}
(2)__attribute__函数的作用:将数组定义在固定的flash空间上。
#if defined (__CC_ARM )
uint8_t m_dfu_settings_buffer[CODE_PAGE_SIZE] __attribute__((at(BOOTLOADER_SETTINGS_ADDRESS)))
__attribute__((used));
#elif defined ( __GNUC__ )
uint8_t m_dfu_settings_buffer[CODE_PAGE_SIZE] __attribute__ ((section(".bootloaderSettings")))
__attribute__((used));
#elif defined ( __ICCARM__ )
__no_init __root uint8_t m_dfu_settings_buffer[CODE_PAGE_SIZE] @ BOOTLOADER_SETTINGS_ADDRESS;
#else
#error Not a valid compiler/linker for m_dfu_settings placement.
#endif
其中#if defined ( __CC_ARM )、defined ( __ICCARM__ )以及defined ( __GNUC__ )
是因为ARM 系列目前支持三大主流的工具链:
ARM RealView (armcc) =>ARM keil/MDK
IAR EWARM (iccarm) =>IAR
GNU Compiler Collection (gcc) =>GCC
例如在core_cm3.h中有如下定义:
/* define compiler specific symbols */
#if defined ( __CC_ARM )
#define __ASM __asm /*!< asm keyword for armcc */
#define __INLINE __inline /*!< inline keyword for armcc */
#elif defined ( __ICCARM__ )
#define __ASM __asm /*!< asm keyword for iarcc */
#define __INLINE inline /*!< inline keyword for iarcc. Only
avaiable in High optimization mode! */
#define __nop __no_operation /*!< no operation intrinsic in iarcc */
#elif defined ( __GNUC__ )
#define __ASM asm /*!< asm keyword for gcc */
#define __INLINE inline /*!< inline keyword for gcc
#endif