为什么编译器初始化这个变量到了错误的价值?这是一个定位的问题? [英] Why does the compiler init this variable to the wrong value? Is this an alignment issue?
问题描述
我与一个嵌入式的C编译器(ARM Cortex-M3的芯片)的工作,它似乎初始化错误值一个结构。为什么会这样?如果它是一个对齐问题,应该不是编译器知道对齐的INT32U到4字节边界?
注:printf的只是抛出字节出来的串口。目前该芯片上没有stdio.h中的实现。
typedef结构
{
INT32U的startTime;
INT16U长度;
INT32U关断时间;
}周期;循环周期=
{
315618000,
1200,
0
};
无效的init()
{
的printf(\\ r \\ nInitialized周期开始:%D,cycle.startTime); cycle.startTime = 315618000;
cycle.length = 1200; 的printf(循环开始数:%d,cycle.startTime);}
输出:
初始化!循环启动:631237200周期开始:315618000
块引用>注意::这难道不是一个printf的问题。调试器验证的631237200以及在内存中的值。
解决方案在某些嵌入式系统,静态初始化没有设置自动发生。这违背C规格,但有时这就是事情是这样的。请注意,这可能是对数据和BSS段,即你可能会发现未初始化的静态可能不会被初始化为零是真。
解决这个问题的解决方案是,不幸的是,特定的系统。你会发现你的编译器的系统文档中的东西,可以让你调用静态元素的初始化。
I'm working with an embedded C compiler (ARM cortex-m3 chip) and it seems to initialize the wrong value to a struct. Why does this happen? If it's an alignment issue, shouldn't the compiler know to align an int32u to a 4-byte boundary?
Note: the printf merely throws bytes out of the serial port. There is no stdio.h implementation on this chip.
typedef struct { int32u startTime; int16u length; int32u offTime; } Cycle; Cycle cycle = { 315618000, 1200, 0 }; void init() { printf("\r\nInitialized! Cycle Start: %d", cycle.startTime); cycle.startTime = 315618000; cycle.length = 1200; printf(" Cycle Start: %d", cycle.startTime); }
Output: Initialized! Cycle Start: 631237200 Cycle Start: 315618000
Note:: This NOT a printf issue. The debugger verifies the value in memory as 631237200 as well.
解决方案In some embedded systems, static initialization is not set up to happen automatically. This goes against C specifications, but sometimes that's the way it is. Note that this may be true for both data and bss segments i.e. you may find that uninitialized statics may NOT be initialized to zero either.
The solution to this is, unfortunately, system specific. You may find something in your complier system documentation that lets you invoke the initialization of the static elements.
这篇关于为什么编译器初始化这个变量到了错误的价值?这是一个定位的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!