简体   繁体   English

为什么数组声明的顺序在嵌入式系统中很重要?

[英]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.

相关问题 为什么数组声明的顺序对性能有如此大的影响? - Why does order of array declaration affect performance so much? JavaScript下划线_.each:为什么在引用对象/数组属性时回调参数的顺序很重要? - JavaScript Underscore _.each: Why does the order of a callback's arguments matter when referencing object/array properties? 为什么缓存局部性对数组性能很重要? - Why does cache locality matter for array performance? 将JSON与嵌套数组和jsons进行比较(数组顺序无关紧要) - Compare JSON with nested arrays and jsons (Array order does not matter) RSpec 希望接收带有数组的方法,但顺序无关紧要 - RSpec expect to receive method with array but order does not matter 多维数组声明中的顺序是否会影响已用内存? - Does order in a declaration of multidimensional array have an influence on used memory? 系统verilog中的打包数组声明 - Packed array declaration in system verilog C编程,为什么这个大型数组声明会产生分段错误? - C programming, why does this large array declaration produce a segmentation fault? 为什么这段代码要求我在 function 声明中包含数组的大小? - Why does this code require me to include the size of the array in the function declaration? 为什么数组大小声明使用“1”作为第一个索引? - Why does array-size declaration use “1” as the first index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM