sprintf corrupting arrays in IAR microcontroller

不羁的心 提交于 2019-12-13 01:26:36

问题


I am currently learning embedded programming, and thus working on an IAR-platform using a TI microcontroller with ARM architecture. Since I am not at all familiar with the technicalities related to this kind of programming, or C programming in general, I would like to ask a basic question:

I have the following simple code snippet:

int i;
for(i = 0; i < NUM_SAMPLES; i++)
{
    sinTable[i] = sinf(2*i*dT*PI);
}


for(i = 0; i < NUM_SAMPLES; i++)
{
    char out[32];
    sprintf(out,"sin: %.7f, %.7f;", i*dT, sinTable[i]);
    putString(out);
    delay(DELAY_100US);
}

Where sinTable[] is a global variable of size NUM_SAMPLES, putString(*char) is a function which writes to an RS232-port, and delay(float) is a simple delay-function.

My problem is that once the sprintf(...) is called, it corrupts sinTable, giving some very peculiar results when plotting the table on the receiver end of the COM-signal.

I don't expect that I run out of memory, as the MC has 64KB SRAM.

Does anyone have any thoughts?


回答1:


Ensure that your stack pointer is on a 64-bit boundary when main is reached.

The symptom your are seeing is typical of a stack aligned on an odd 32-bit boundary. Everything seems to work properly until a double is used as a variadac argument. This breaks when the code expects such arguments to be on 8-byte boundaries.




回答2:


Upon further review: I suspect that Michael Burr's response regarding stack utilization was on the right track. Selecting a smaller printf library might be sufficient, but if you can increase your stack size, that seems safer. Note that the IAR C/C++ Development Guide includes info on linker stack usage analysis.

Original: When I upgraded from IAR 6.1 (licensed) to 6.4 (kickstart), I ran into a similar problem - vsnprintf was writing "all over RAM", even though the return value indicated the number of characters written was well within the target bounds. The "solution" was to avoid the printf library that has multibyte support.

Project Options > General > Library Options > printf small w/o multi-byte

might want to also uncheck

Project Options > C/C++ Compiler / Language 2 / enable multibyte

I tried to report this to IAR, but since my support contract is expired ...

Unfortunately, a similar problem is back with IAR 7.3.4, and the multibyte "fix" does NOT seem to be sufficient. Happens with both sprintf() and snprintf(), although the out-of-bounds corruption is not identical between those 2.




回答3:


You seem pretty confident that your result string is only 31 characters long. The only way to corrupt another variable with your sprintf statement is that your result string is longer than 32 bytes (31 chars and a nul-byte), therefore overwriting other parts of memory. Make your numbers smaller or make your temporary buffer larger.




回答4:


Thank you to anyone who have suggested a solution to this problem. I eventually ended up writing a conversion method that gives a hex-representation of the string and transmitted that instead, omitting the sprintf(...) completely.

It is very crude, but suits my needs.



来源:https://stackoverflow.com/questions/22295028/sprintf-corrupting-arrays-in-iar-microcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!