I saw a code sample demonstrating the usage of the `volatile` qualifier in the answer of the question [C++ volatile member functions](https://stackoverflow.com/questions/4826719/c-volatile-member-functions), quoted as below:

```
volatile int x;

int DoSomething() {
    x = 1;
    DoSomeOtherStuff();
    return x+1; // Don't just return 2 because we stored a 1 in x.  
                // Check to get its current value
}

```

I don't know if the `volatile` qualifier makes any difference in the above code. `x` is a global variable and there is a function call between the write and read on `x`, and we only read `x` for once. Shouldn't the compiler perform a real read on `x` (even it's not `volatile`)?

I think it's different from the following case:

```
volatile int x;

int DoSomething() {
    x = 1;
    while (x != 1)
        break;
}

```

In this case, we repeatedly read `x` immediately after a write to `x`, so `volatile` is necessary to get the up-to-date value of `x` written by other threads.

I'm not very confident on my understanding of these code samples, please correct me if I'm wrong.

EDIT (respond to comments): I'm sorry I didn't make my question clear. As for the first code snippet, I'm questioning whether the code is a valid example for the **possible** usage of `volatile` (not the **guaranteed** usage of volatile). I just want to know, without `volatile`, whether it's guaranteed that any possible change to `x` in `DoSomeOtherStuff()` can be reflected in `return x+1`, assuming there's no multi-threading or other non-trivial things like memory mapped IO. Cause if it is guaranteed to work without `volatile`, then the example is way off relevant, not to even mention the platform-dependent nature of `volatile` as some comments pointed out. But if it's not guaranteed, then I'm afraid some of my existing code may not work as I expected.  

(I probably shouldn't have put the second code snippet at all.)