在固定内存位置定义结构或数组成员 [英] Defining structure or array members at fixed memory locations

查看:78
本文介绍了在固定内存位置定义结构或数组成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在结构表中定义了一些常量,我希望在链接器时间(静态地,非动态地)使用以下模式放置在内存中:



假设我有一个包含3个 StructType 类型结构的表,例如 StructType MyVar [3]; 。 StructType的大小为20个字节。



I have defined sets of constants in a structure table that I want to place in memory at linker time (so statically, not dynamically) using the following pattern:

Assuming I have a table of 3 structs of type StructType e.g. StructType MyVar[3];. The StructType has a size of 20 bytes.

Address   : Content
0xA000    : MyVar[0]
0xA100    : MyVar[1]
0xA200    : MyVar[2]





每个结构后跟236(= 0x100 - 20d)为空示例中的字节。



我知道这很依赖于使用过的链接器/编译器的具体指令,但有没有办法实现呢?

以下结果会得到相同的结果,但是使用不同的变量而不是表:



Each struct is followed by 236 (=0x100 - 20d) empty bytes in the examples.

I know this relies a lot on the specific instructions of the used linker/compiler, but is there a way to achieve that ?
The following would end up with the same result, but with different variables instead of a table:

const StructType MyVar1 __attrib(0xA000) = bla foo;
const StructType MyVar2 __attrib(0xA100) = bla foo;
const StructType MyVar3 __attrib(0xA200) = bla foo;





感谢您的建议!



Thanks for your advice !

推荐答案

类似

It's something like
const StructType * myVar1 = reinterpret_cast<StructType*>(0xA000);
// ...





此外,您可能会面临一些问题,即使用 struct 对单独成员进行对齐以及填充的可能性为了访问性能,存储器在结构中具有未使用的存储器片段。关于这个问题,请参阅: https://en.wikipedia.org/wiki/Data_structure_alignment [
http://www.catb.org/esr/structure-packing [ ^ ],

https://evpo.wordpress.com/ 2014/01/25 / memory-alignment-of-structures-and-classes-in-c-2 [ ^ ],

http://www.drdobbs.com/cpp/padding-and-rearranging-structure-会员/ 240007649 [ ^ ]。



-SA



Also, you can face some problems with alignment of separate members withing your struct and possibility of padding the memory in structure with unused memory fragments, for the sake of access performance. On this problem, please see: https://en.wikipedia.org/wiki/Data_structure_alignment[^].

See also:
http://www.catb.org/esr/structure-packing[^],
https://evpo.wordpress.com/2014/01/25/memory-alignment-of-structures-and-classes-in-c-2[^],
http://www.drdobbs.com/cpp/padding-and-rearranging-structure-member/240007649[^].

—SA


没有解决方案你解释事情的方式。



我认为你向后解决问题,你问如何使工作成为你找到的解决方案,而不是提出你的问题和询问哪种解决方法最好。



BonjourRégis,

a mon avis,tu prendstonprolèmeàl'envers。 Au lieu de demander评论faire marcher ta解决方案,tu devraispluttt expliquer ce que tu veux faire,et on te dirai comment faire。



Delafaçondonttu pose ta问题,je vois que tu asuneidéederrièrelapêtequetu n'as pasexpliqué。 C'est justement en expliquanttonprollèmequ'onpourra te donner une solution qui marche。
There is no solution the way you explained things.

I think you take the problem backward, you ask how to make work the solution you found, instead of presenting your problem and asking which is the best way to solve it.

Bonjour Régis,
A mon avis, tu prends ton problème à l'envers. Au lieu de demander comment faire marcher ta solution, tu devrais plutôt expliquer ce que tu veux faire, et on te dirai comment faire.

De la façon dont tu pose ta question, je vois que tu as une idée derrière la tête que tu n'as pas expliqué. C'est justement en expliquant ton problème qu'on pourra te donner une solution qui marche.


这篇关于在固定内存位置定义结构或数组成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆