简体   繁体   English

传递给函数的参数已损坏

[英]Corrupted arguments passed to function

I am writing an executable for an embedded device (an STM32) in C. After some debugging, I have reduced it to this function: 我正在用C为嵌入式设备(STM32)编写可执行文件。经过一些调试后,我将其简化为以下功能:

char * parse(char * start)
{
    int i = 0;
    char command[20];
    print(start);
}

For some reason, when I call this function, the argument start is corrupted. 由于某种原因,当我调用此函数时,参数start损坏。 Now I can get it to work if I comment out the command initialisation: 现在,如果我注释掉command初始化,则可以使它工作:

char * parse(char * start)
{
    int i = 0;
    // char command[20];
    print(start);
}

With command commented out, it all works fine. 注释掉command后,一切正常。 I was thing that maybe I might be running out of RAM. 我可能无法用完RAM。 But this is program is tiny, and upon checking the stack pointer register, I can confirm that I have a lot of RAM space left. 但这是程序,很小,检查堆栈指针寄存器后,我可以确认我还有很多RAM空间。

What could be going wrong here? 这里可能出什么问题了? A broken compiler? 损坏的编译器? (I'm using a recompiled version of GCC for the ARM called Yagarto.) (我正在为ARM使用名为Yagarto的GCC的重新编译版本。)

More than likely you're in the area of undefined behaviour because you've done something wrong elsewhere in your program. 您很可能属于不确定行为的领域,因为您在程序的其他地方做错了什么。 The fact that it works under some circumstances in no way makes undefined behaviour acceptable :-) 它在某些情况下不能工作的事实使未定义的行为可以接受:-)

Possibly, you've overwritten memory or your string is not null terminated, or any of a hundred other reasons. 可能是您覆盖了内存,或者您的字符串不是以null结尾的,或者是其他一百种原因。

You somehow destroyed the parse function's stack. 您以某种方式破坏了parse函数的堆栈。 Commenting out the declaration of command variable will remove the allocation on the 20 bytes in your stack that implicitly happens if you keep it. 注释掉command变量的声明将删除堆栈中20个字节的分配,如果保留该分配,则会隐式发生。

Can you try Valgrind on your program to find out where the corruption happens ? 您可以在程序上尝试Valgrind找出损坏发生的地方吗?

Also, what is the print function ? 另外,什么是print功能? Try printing the value of start in both cases with 在两种情况下尝试打印start的值

printf("%p\n", start);

You can find the stack of your function in the memory at the address pointed by parse 您可以在内存中由parse指向的地址处找到函数堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM