Click here to Skip to main content
15,028,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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]


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:
C++
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 !
Posted
Comments
Richard MacCutchan 20-Aug-15 13:01pm
   
Just add a dummy byte array of the required length in the structure.
Sergey Alexandrovich Kryukov 20-Aug-15 13:16pm
   
This is a correct know technique, but this is not the inquirer's concern. It's all about absolute fixed memory location of an object.
Anyway, I answered the question...
—SA
Rage 21-Aug-15 3:15am
   
To be honest, Richard's idea was the solution I had in mind should I not find anything else. But it is a little bit of a hassle for us, since you need to compute the size of the "empty" array when the size of the structure changes... Hence looking at a "proper" solution first.

I'll have a look at your links, thank you !
Sergey Alexandrovich Kryukov 21-Aug-15 10:42am
   
I understand, but the question and you "pseudo-code" sample suggested that your major problem was to specify fixed addresses like 0xA000 statically. I answered exactly. So, will you accept my answer formally? (You can accept more than one.)
—SA
Rage 21-Aug-15 11:04am
   
Wow, I did not know that it was that important. Ok, here, get your points.

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


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
   
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.
   
v2
Comments
Rage 21-Aug-15 3:11am
   
Hello, thanks for your concern. I think I exactely explained what I need, namely put identical data structures in fixed places in memory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900