ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Storage Organization S…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 27 of 32
    Topic Study Note Illustration
    27

    Storage Organization and Stack Allocation of Space

    This topic belongs to the Runtime Environment / Code Generation phase of a compiler. It explains how memory is organized when a program runs and how space...

    🧠 Storage Organization and Stack Allocation of Space (Compiler Construction)

    This topic belongs to the Runtime Environment / Code Generation phase of a compiler. It explains how memory is organized when a program runs and how space is allocated for functions, variables, and procedures.


    πŸ“Œ 1. Storage Organization

    πŸ“– Definition

    Storage organization refers to how memory is arranged and managed during program execution.

    πŸ‘‰ It divides memory into different regions to store:

    • Program code
    • Variables
    • Function calls
    • Dynamic data

    🧠 2. Memory Layout of a Running Program

    A typical program memory is divided into 4 main sections:


    πŸ“Š Memory Layout Diagram

    +----------------------+
    | Text Segment        |  ← Program code (instructions)
    +----------------------+
    | Static / Data Area  |  ← Global & static variables
    +----------------------+
    | Heap                |  ← Dynamic memory (malloc/new)
    +----------------------+
    | Stack              |  ← Function calls, local variables
    +----------------------+
    

    πŸ”Ή 1. Text Segment

    • Stores program instructions
    • Read-only

    πŸ”Ή 2. Data Segment

    • Stores global variables
    • Static variables

    πŸ”Ή 3. Heap

    • Dynamic memory allocation
    • Grows upward

    Example:

    malloc(), new
    

    πŸ”Ή 4. Stack

    • Stores function calls
    • Local variables
    • Grows downward

    πŸ“Œ 3. Stack Allocation of Space

    πŸ“– Definition

    Stack allocation is a method of allocating memory for function calls in a Last In First Out (LIFO) manner using a stack.

    πŸ‘‰ Each function call gets a block called:

    Activation Record (Stack Frame)


    🧠 4. Activation Record (Very Important)

    πŸ“– Definition

    An activation record is a block of memory used to store information about a function call.


    πŸ“Š Structure of Activation Record

    +----------------------+
    | Parameters          |
    +----------------------+
    | Return Address      |
    +----------------------+
    | Control Link        |
    +----------------------+
    | Access Link         |
    +----------------------+
    | Local Variables     |
    +----------------------+
    | Temporary Values    |
    +----------------------+
    

    πŸ”Ή Explanation of Fields

    1. Parameters

    • Input arguments of function

    2. Return Address

    • Where to return after function execution

    3. Control Link

    • Points to previous activation record

    4. Access Link

    • Used for nested functions (static scope)

    5. Local Variables

    • Variables declared inside function

    6. Temporaries

    • Intermediate results

    βš™οΈ 5. Stack Allocation Process

    Step-by-step execution:

    πŸ”Ή Step 1: Function Call Occurs

    Memory block (activation record) is created

    πŸ”Ή Step 2: Push on Stack

    New frame is pushed

    πŸ”Ή Step 3: Execution Happens

    Function executes using its frame

    πŸ”Ή Step 4: Return

    Frame is popped from stack


    πŸ“Š Stack Behavior Diagram

    | Function C |
    | Function B |
    | Function A |  ← top of stack
    

    When C finishes:

    | Function B |
    | Function A |
    

    🧠 6. Example of Stack Allocation

    int main() {
       int x = 10;
       fun();
    }
    
    void fun() {
       int y = 20;
    }
    

    Execution:

    1. main() activation record created
    2. fun() called β†’ new activation record pushed
    3. fun() completes β†’ popped
    4. return to main()

    πŸ“Œ 7. Properties of Stack Allocation

    βœ” Fast allocation and deallocation βœ” Follows LIFO order βœ” Memory reused automatically βœ” Used for function calls βœ” Efficient for recursive programs


    ⚠️ 8. Limitations of Stack Allocation

    ❌ Cannot handle dynamic data (fully flexible structures) ❌ Limited lifetime of variables ❌ Not suitable for objects needing long-term storage

    πŸ‘‰ That’s why heap is also used.


    🧠 9. Stack vs Heap (Very Important Exam Comparison)

    Feature Stack Heap
    Allocation Automatic Manual
    Speed Fast Slower
    Structure LIFO Random
    Lifetime Temporary Long-term
    Used for Functions Dynamic memory

    πŸ“Œ 10. Key Exam Points

    βœ” Memory is divided into text, data, heap, stack βœ” Stack stores function calls and local variables βœ” Each function uses Activation Record βœ” Stack follows LIFO order βœ” Stack allocation is fast and automatic βœ” Used heavily in recursive function calls


    🎯 Final Exam Definition

    Storage organization refers to how memory is divided into segments like text, data, heap, and stack during program execution. Stack allocation of space is a runtime memory management technique where function calls are handled using a stack, and each call creates an activation record containing parameters, return address, local variables, and control information.


    πŸ“Š FINAL REVISION TABLE

    Concept Description
    Storage organization Memory layout of program
    Stack Stores function calls
    Heap Dynamic memory
    Data segment Global/static variables
    Activation record Function call memory block
    Stack allocation LIFO memory management

    ← Previous topic 26
    Back Patching and Switch Statements
    Next topic 28 β†’
    Heap Management and Optimization

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    683words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction