Memory data order

A few simple questions.

const int gFirst;
const int gSecond;

struct Data
{
    static int First;
    static int Second;

    int first;
    int second;
};

Data data;

Is it guaranteed that the following statements are true?

  • &gFirst < &gSecond
  • &Data::First < &Data::Second
  • &data.first < &data.second
+5
source share
2 answers

1) This result is not specified.
2) This result is unspecified. *
3) Yes.

The relevant section in the standard is §5.9 / 2. Relational comparisons between pointers pand are qindicated only when:

  • pand qpoint to the same object or function, point to one end of the end of the same array, or both values ​​are zero. In this case, p <= qthey p >= qare both true p < qand p > qfalse.
  • p q , . ( : .)
  • p q , .
  • p q , .

.

* , () " ". , , , . (.) >


! , std::less<void*> ( .)

§20.3.3/8:

greater, less, greater_equal less_equal , <, >, <=, >= .

, , std::less<void*>(&gFirst, &gSecond) true false, :

std::less<void*>(&gFirst, &gSecond) ==
    std::greater<void*>(&gSecond, &gFirst);
std::less<void*>(&Data::First, &Data::Second) ==
    std::greater<void*>(&Data::Second, &Data::First);

.

+8

:

1) Not guaranteed.
   But probably.
   But the order of initialization is guaranteed.
2) No. 
   You just have the declaration here.  
   You need to to define the instances in a source file.
   Then they will behave the same way as the objects in (1).
3) Yes.
+6

All Articles