[英]Why does the order of array declaration matter in an embedded system?
I'm writing some code for a class and I need to manipulate some user input.我正在为 class 编写一些代码,我需要处理一些用户输入。 The input is a user string that contains a hex value for a memory address and a word/phrase up to 16 characters long.
输入是一个用户字符串,其中包含 memory 地址的十六进制值和最多 16 个字符长的单词/短语。 Each character after the memory address is then converted to a hex value and that value is then used to set the byte value at the memory address specified.
然后将 memory 地址之后的每个字符转换为十六进制值,然后使用该值设置指定 memory 地址处的字节值。
Cmd *new_cp = &cmds[1];
int memAddrs_int = 0;
char userString [16] = "";
char memAddrs_str [16] = "";
char argsToPass [16] = "";
char byteValue [2] = "";
This code copiles and runs just fine on a linux environment but when I run it in a VM that simulates an embedded system, I get a page fault exception.此代码在 linux 环境中编译和运行得很好,但是当我在模拟嵌入式系统的 VM 中运行它时,出现页面错误异常。 While renaming some variables I changed the order of the arrays to:
在重命名一些变量时,我将 arrays 的顺序更改为:
Cmd *new_cp = &cmds[1];
int memAddrs_int = 0;
char userString [16] = "";
char argsToPass [16] = "";
char memAddrs_str [16] = "";
char byteValue [2] = "";
and now everything works as expected.现在一切都按预期进行。 Any ideas as to why the order that the arrays were declared and initialized would matter on an embedded system?
关于为什么声明和初始化 arrays 的顺序对嵌入式系统很重要有什么想法吗? Or would this have something to do with the fact that it is running on a VM?
或者这与它在 VM 上运行的事实有关吗?
It is not the order of declaration that is causal here.这里的因果关系不是声明的顺序。 Rather some memory access error elsewhere having a different effect because the declaration order affects what adjacent data object is being corrupted and later accessed and used.
而是一些 memory 访问错误在其他地方具有不同的影响,因为声明顺序会影响相邻数据 object 被损坏以及稍后访问和使用的内容。
Somewhere in your code you have a bug with "undefined behaviour" results, and any change, including declaration order of unrelated data objects can affect actual behaviour .在您的代码中的某处,您有一个带有“未定义行为”结果的错误,并且任何更改(包括不相关数据对象的声明顺序)都会影响实际行为。 The behaviour could even change spontaneously with no changes.
行为甚至可以自发地改变而没有改变。
You have你有
char byteValue [2] = "";
If the byteValue
array is intended to hold strings like "3d"
(as you confirmed in a comment that it is), then it needs to be declared with three elements, including one for the necessary null terminator:如果
byteValue
数组旨在保存像"3d"
这样的字符串(正如您在评论中确认的那样),那么它需要用三个元素声明,其中一个用于必要的 null 终止符:
char byteValue [3] = "";
When it was declared with a size of 2, and when some code wrote three bytes there and overflowed it, some other piece of memory got overwritten.当声明它的大小为 2 时,当一些代码在那里写入三个字节并溢出时,memory 的其他部分被覆盖。 We don't know what piece of memory that was, or what the effect was — benign or catastrophic or somewhere in between — but it's not too surprising that changing the order of the declarations changed the circumstances.
我们不知道那是 memory 的哪一部分,也不知道其影响是什么——良性的或灾难性的或介于两者之间——但改变声明的顺序改变了情况也就不足为奇了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.