What does “Memory allocated at compile time” really mean?

前端 未结 13 1040
时光取名叫无心
时光取名叫无心 2020-12-04 04:30

In programming languages like C and C++, people often refer to static and dynamic memory allocation. I understand the concept but the phrase \"All memory was allocated (rese

相关标签:
13条回答
  • 2020-12-04 05:37

    Memory can be allocated in many ways:

    • in application heap (whole heap is allocated for your app by OS when the program starts)
    • in operating system heap (so you can grab more and more)
    • in garbage collector controlled heap (same as both above)
    • on stack (so you can get a stack overflow)
    • reserved in code/data segment of your binary (executable)
    • in remote place (file, network - and you receive a handle not a pointer to that memory)

    Now your question is what is "memory allocated at compile time". Definitely it is just an incorrectly phrased saying, which is supposed to refer to either binary segment allocation or stack allocation, or in some cases even to a heap allocation, but in that case the allocation is hidden from programmer eyes by invisible constructor call. Or probably the person who said that just wanted to say that memory is not allocated on heap, but did not know about stack or segment allocations.(Or did not want to go into that kind of detail).

    But in most cases person just wants to say that the amount of memory being allocated is known at compile time.

    The binary size will only change when the memory is reserved in the code or data segment of your app.

    0 讨论(0)
提交回复
热议问题