How the memory layout of global variable is reliable ?

Cjkp via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 22 13:29:57 PDT 2014


Hello, I have an idea about a small code tool related to the 
application resources.
It would rely on the assumption that some global variabled, 
sharing the same type and attributes, declared in group, are 
contiguous.

In short I need to know if the following assertions are always 
true and reliable over time:

--------------
import std.stdio;

// used as base adress
static string beg = "";
// arbitrary generated by a tool and mixed at compile time.
static string a = "";
static string b = "";
static string c = "";
static string d = "";
static string e = "";
static string f = "";

void main(string args[])
{
     void* offs = &beg;
     assert( &a == (offs + (size_t.sizeof * 2) * 1) ); // length + 
ptr
     assert( &b == (offs + (size_t.sizeof * 2) * 2) ); // length + 
ptr
     assert( &c == (offs + (size_t.sizeof * 2) * 3) ); // etc.
     assert( &d == (offs + (size_t.sizeof * 2) * 4) );
}
--------------

In a second time I need to be sure that the return tuple of the 
trait "allMembers" follow the declarations order. The 
documentation says that:

"The order in which the strings appear in the result is not 
defined".

But so far, it looks like it's ordered according to the 
declaration (at least for a module containing only some global 
variables).

Any other remarks about the topic are welcome.


More information about the Digitalmars-d-learn mailing list