[CS 61] Data Representation (2)

Kieun Kim·2021년 9월 2일

CS 61 SPMO 2020

목록 보기
3/3

Segments

  • Lifetime : storage duration which an object has by the standard

    • static life time : The object lasts as long as the program runs
      • a global variable, a constant global variable
    • automatic life time : The compiler allocates and destroys the object automatically as the program runs based on the object's scope
      • a local variable
        (Object's scope : the region of the program in which it is meaningful)
    • dynamic life time : The programmer allocates and destroys the object explicitly
  • Segments : Regions divided into a programs's address space

    • Objects with diffrent lifetimes are placed into different segments
    • The kind of most important segments
      • Code : It also known as text or read-only-data. It contains instructions and constant global objects. Unmodifiable. Static lifetime
      • Data : Contains non-constant global objects. Modifiable. Static lifetime
      • Heap : Modifiable. Dynamic lifetime
      • Stack : Modifiable. Automatic lifetime
  • The compiler decides on a segment for each object based on its lifetime

  • The linker (final complier phase) groups all the program's objects by segment

  • Finally, when a program runs, the operating system loads the segements into memory ( The stack and heap segments grow on demand )

    Object decalarationLifetimeSegmentExample address range
    Constant globalStaticCode(or text)0x40'0000(similar to 1 * 2^22)
    GlobalStaticData0x60'0000(similar to 1.5 * 2^22)
    LocalAutomaticStack0x7fff'448d'0000(similar to 2^47 = 2^25 * 2^22)
    Anonymous, returned by newDynamicHeap0x1a0'0000(similar to 8 * 2^22)
  • Constant global data and global data have the same life time but are stored in different segments (The operating system uses different segments so it can prevent the program from modifying constatnts)

  • An executable is normally at least as big as the static-lifetime data

  • All that data must be in memory for the entire lifetime of the program, it's written to disk and then loaded by the OS before the program starts running

  • bss segment is used to hold modifiable static-lifetime data with initial value zero

profile
Connecting dots

0개의 댓글