How does compiler lay out code in memory

后端 未结 3 831
独厮守ぢ
独厮守ぢ 2020-12-06 02:56

Ok I have a bit of a noob student question.

So I\'m familiar with the fact that stacks contain subroutine calls, and heaps contain variable length data structures, a

3条回答
  •  伪装坚强ぢ
    2020-12-06 03:16

    It depends.

    If you're compiling a bootloader, which has to start from scratch, you can assume you've got the entire memory for yourself.

    On the other hand, if you're compiling an application, you can assume you've got the entire memory for yourself.

    The minor difference is that in the first case, you have all physical memory for yourself. As a bootloader, there's nothing else in RAM yet. In the second case, there's an OS in memory, but it will (normally) set up virtual memory for you so that it appears you have the entire address space for yourself. Usuaully you still have to ask the OS for actual memory, though.

    The latter does mean that the OS imposes some rules. E.g. the OS very much would like to know where the first instruction of your program is. A simple rule might be that your program always starts at address 0, so the C compiler could put int main() there. The OS typically would like to know where the stack is, but this is already a more flexible rule. As far as "the heap" is concerned, the OS really couldn't care.

提交回复
热议问题