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

前端 未结 13 1127
时光取名叫无心
时光取名叫无心 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:26

    I would like to explain these concepts with the help of few diagrams.

    This is true that memory cannot be allocated at compile time, for sure. But, then what happens in fact at compile time.

    Here comes the explanation. Say, for example a program has four variables x,y,z and k. Now, at compile time it simply makes a memory map, where the location of these variables with respect to each other is ascertained. This diagram will illustrate it better.

    Now imagine, no program is running in memory. This I show by a big empty rectangle.

    empty field

    Next, the first instance of this program is executed. You can visualize it as follows. This is the time when actually memory is allocated.

    first instance

    When second instance of this program is running, the memory would look like as follows.

    second instance

    And the third ..

    third instance

    So on and so forth.

    I hope this visualization explains this concept well.

提交回复
热议问题