Memory Layout of application
The following question is a head-scratcher for me. Assuming that I have two platforms with an 开发者_JAVA技巧identical hardware, the same OS and the same compiler on it. If I compile exactly the same application, can I be sure that the memory layout on both machines will exactly be the same? In other words, both applications have exaclty the same virtual address space or is there a high chance that this is not the case.
Thanks for your thoughts about this!
You can't count on it. As a security feature, some OS's (including Windows) randomize memory layout to some extent.
(Here's a supporting link: http://blogs.msdn.com/b/winsdk/archive/2009/11/30/how-to-disable-address-space-layout-randomization-aslr.aspx)
It is highly improbable that an application will be executed in the same address space on the same platform, nonetheless on another computer. Other applications may be running which will affect where the OS loads your application.
Another point to consider is that some applications load run-time libraries (a.k.a. DLLs & shared libraries) on demand. An application may have a few DLLs loaded or not when your application is running.
In non-embedded platforms, the majority of applications don't care about exact physical memory locations, nor is it a concern that they are loaded in the same location each time. Most embedded platforms load their applications in the same place each time, as they don't have enough memory to move it around.
Because of these cases and the situations other people have mentioned, DO NOT CODE CONSTANT MEMORY LOCATION principles into your program. Very bad things will happen, especially difficult to trace and debug.
Apart from dynamic question such as stack addresses as Steven points out, there is also the aspect of compile time and static layout.
Already I think that two machines are exact clones of each other is a very particular situation, since you may have tiny differences in CPU version, libraries etc. Then some compilers (perhaps depending on some options) also put the compile time and date in the executable. If e.g your two hostnames have different lengths or this uses a date format that varies in length, not only these strings will be different, but all other static variables might be slightly shifted in address space.
I remember that gcc
had difficulties on some architectures with its automatic build since the compiler that was produced in stage 2 was differed of the one build in stage 3 for such stupid reasons.
The __TIME__
macro expands to (the start of) the compilation time. Furthermore, it's determined
independently for each and every .cpp file that you compile, and the linker can eliminate duplicate strings.
As a result, depending on the compile speed, your executables may end up not just with different __TIME__
strings, but even a different number of __TIME__
strings.
If you're working late, you could see the same with __DATE__
strings ;)
Is it possible for them to have the same memory layout? Yes, it is a possibility. Is it probable? Not really.
As others have pointed out, things like address space randomization and __TIME__
macros can cause the address space to differ (whether the changes were made at compile time or run time). In my experiences, many compilers don't produce the same identical output when run twice on the same machine using the exact same input (functions are laid out in memory in different orders, etc).
Is this a rhetorical/intellectual question, or is this causing you to run into some kind of problem with a program you are writing?
精彩评论