Is there any difference in the memory layout of Windows and Linux OS?

c, layout, linux, memory, windows

Solution

Yes, windows and linux lay out their memory differently. Some examples are here. For example, windows typically splits your memory evenly (in 32-bit) between kernel and user space, while linux is 3/1 user/kernel.

The compiler can also lay out the memory as it sees fit, within the limits of the spec. This means that the llvm compiler, gcc, and ever different versions of these can have different output.

Optimizations can also change layout, and even remove some variables that aren't strictly needed.

Also, even if memory was allocated low to high, after some other memory was freed a new allocation could come from the previously used area and be low again.

Short answer: expecting memory layout/location between unrelated variables is not a good idea.

Problem

When I run the code written below on windows and on Linux, I get different output for the two. I am using gcc for both. When I run it on windows, I get "Seek" as output whereas running it on Linux, I am getting "Hide" as the output. Is there any difference in the memory layout of Windows and Linux, or is there something else which causes the output to differ? ``` int main() { int a=0; int *b=(int *)malloc(sizeof(int)); if(&a>b) printf("Hide"); else printf("Seek"); return 0; } ```

Original source