views:

112

answers:

1

I have read Static Memory Allocation are done during Compile time.

Is the 'address allocated' used while generating executables ?

Now, I am in doubt how the memory allocation is handled when the code executable is transferred completely onto a new system.

I searched for it but I didn't get any answer on the internet.

+2  A: 

Well, it totally depends on the circumstances whether your executable can be run on your new system or not. Each operating system defines it's own exectuable file format. For example, here's how windows exe's look like. There's a reason why they are called portable executable.

When your compiler generates such an executable, it does first compile your C code to the corresponding assembly of your target architecture and then packs it into the target executable file format. Static memory allocations find their place in that format.

You can imagine the exe file as sort of a memory image that is loaded into a new memory space by the operating systems process loader. The operating system maintains the offset to this location and makes sure all the programs memory access goes into it's process' protected address space.

To answer your specific question: Transferring an executable between systems of the same operating system and architecture is usually no problem. The scenario same OS but different machine architecture can usually be handled by the OS via emulation (e.g. Mac OS's Rosetta emulates PowerPC on x86). 64/32 bit compatibility is handled this way too. Transferring between different OS's is usually not possible (for native executables) but everything that works inside virtual machines (java vm, .net CLR) is no problem, as the process loader loads only the virtual machine and the actual program is run from there.

Johannes Rudolph
Can you explain "Same OS but different machine architecture" case....? What actually is the mechanism in that case?
Prasoon Saurav
A: MacOS on PowerPc (old macs) or Intel (new Macs). On a new intel mac, old PowerPC code can be run via emulation of a PowerPC CPU.B: Windows on x86 and x86-64 (32bit/64bit): Maintain backwards compatibility to 32bit code by providing virtual address space that can be addressed with 32bit pointers.
Johannes Rudolph