[static] [shared] [const|immutable]

Lionello Lunesu lionello at lunesu.remove.com
Thu May 14 01:09:36 PDT 2009


I like shared/const/immutable as much as the next guy, but there are now 
2x2x3=12 ways to decorate a variable. Furthermore, by either declaring the 
variable globally or locally (stack), we end up with 24 possible 
declaration. See the code at the end of this post.

Surely, some combinations should/can be disallowed?
 * What's the point of "shared immutable"?
 * What about a locally declared "shared" variable? (The stack is implicit 
TLS anyway.)
 * Come to think of it, what's the difference between a "const int" and a 
"immutable int"? (Is "const" only meant to be used in a function's argument 
list?)

L.

(From the results it's apparent that "static" on the global level does not 
actually do anything (probably kept for C/C++ compatibility where it's used 
to mean "private") and that "static" used locally declares the variable as 
if it were global. This makes sense.)

//The code:

int f = 0xDEAD0001;     //0678
const int cf = 0xDEAD0002;    //068d
immutable int _if = 0xDEAD0003;    //068d

shared int sf = 0xDEAD0011;    //068d
shared const int scf = 0xDEAD0012;   //068d
shared immutable int sif = 0xDEAD0013;   //068d

static int Sf = 0xDEAD0101;    //0678
static const int Scf = 0xDEAD0102;   //068d
static immutable int Sif = 0xDEAD0103;   //068d

static shared int Ssf = 0xDEAD0111;   //068d
static shared const int Sscf = 0xDEAD0112;  //068d
static shared immutable int Ssif = 0xDEAD0113;  //068d

int main()
{
  int f_ = 0xDEAD1001;     //073c**
  const int cf_ = 0xDEAD1002;    //073c*
  immutable int if_ = 0xDEAD1003;   //073c*

  shared int sf_ = 0xDEAD1011;    //073c**
  shared const int scf_ = 0xDEAD1012;   //073c*
  shared immutable int sif_ = 0xDEAD1013;  //073c*

  static int Sf_ = 0xDEAD1101;    //0678
  static const int Scf_ = 0xDEAD1102;   //068d
  static immutable int Sif_ = 0xDEAD1103;  //068d

  static shared int Ssf_ = 0xDEAD1111;   //068d
  static shared const int Sscf_ = 0xDEAD1112;  //068d
  static shared immutable int Ssif_ = 0xDEAD1113; //068d

  // prevent optimization
  return f+cf+_if+sf+scf+sif+Sf+Scf+Sif+Ssf+Sscf+Ssif+
    f_+cf_+if_+sf_+scf_+sif_+Sf_+Scf_+Sif_+Ssf_+Sscf_+Ssif_;
}

// The numbers in comments represent the sections as printed by dumpbin:
//0678  LED386 (TLS?)
//068d  LED386 (global)
//073c  CMD386 (code)
//* only appears in obj when compiled with -g
//** does not appear in obj when compiled with -O



More information about the Digitalmars-d-learn mailing list