我试图找到一种好方法来将变量与32位或16位的GCC对齐.
我正在研究一种不支持未对齐数据访问的Cortex-M0.我使用时遇到这个问题-Os.我使用arm-none-eabi版本4.6.2.我有这个小测试用例:
#include "stdint.h"
typedef struct Struct_HdrPrefix{
uint8_t Prefix;
uint16_t Type;
}__attribute((packed))Struct_HdrPrefix;
volatile Struct_HdrPrefix test;
volatile Struct_HdrPrefix test1;
int main(void)
{
test.Type = 0;
while(1)
__asm("nop");
}
void HardFault_Handler(void)
{
while(1)
__asm("nop");
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我得到一个非对齐的变量测试.
这是地图文件中的结果:
COMMON 0x20000000 0x6 ./SRC/main.o
0x20000000 test1
0x20000003 test
Run Code Online (Sandbox Code Playgroud)
所以现在我陷入了困境.如果我将此添加到vars:
volatile Struct_HdrPrefix test __attribute((aligned (4)));
volatile Struct_HdrPrefix test1 __attribute((aligned (4)));
Run Code Online (Sandbox Code Playgroud)
这是按预期工作的,因为test现在已经调整好了.
我不能在结构上使用align属性,因为这个结构也可能是另一个结构的一部分.
有没有办法告诉GCC或LD在32位边界上对齐打包变量?
问候
我尝试使用此代码获取字符串"-a"的位置
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}
Run Code Online (Sandbox Code Playgroud)
这产生了这个输出:
position found = 4294967295
Run Code Online (Sandbox Code Playgroud)
如果我删除' - '它按预期工作.返回8.