0% found this document useful (0 votes)
9 views4 pages

Atcd5 Mid

The document discusses various storage allocation strategies, including Heap, Static, and Stack storage allocation, highlighting their advantages and disadvantages. Heap allocation allows dynamic memory management at runtime but can lead to memory leaks and is slower than stack allocation. It also explains intermediate-code representations in compiler design, specifically Postfix Notation, Three-Address Code, and Syntax Trees, detailing their structures and examples.

Uploaded by

227r1a1260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Atcd5 Mid

The document discusses various storage allocation strategies, including Heap, Static, and Stack storage allocation, highlighting their advantages and disadvantages. Heap allocation allows dynamic memory management at runtime but can lead to memory leaks and is slower than stack allocation. It also explains intermediate-code representations in compiler design, specifically Postfix Notation, Three-Address Code, and Syntax Trees, detailing their structures and examples.

Uploaded by

227r1a1260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT_5

1)Discuss the advantages and disadvantages of Heap Storage Allocation Strategy?

Heap Storage Allocation:

Heap storage allocation is another form of dynamic memory allocation, but unlike the stack, it allows
memory to be allocated at runtime for variables that can grow or shrink during execution. The heap is
used for memory that is not automatically deallocated when a function call ends. This type of memory
allocation is used when you need to store objects or data that are not tied to a specific function scope.

In C/C++, functions like malloc() or new are used to allocate memory in the heap, and functions like
free() or delete are used to deallocate the memory.

int* ans = new int[5];

Advantages of Heap Storage Allocation

1. Heap allocation is useful when we have data whose size is not fixed and can change during the
run time.

2. We can retain the values of variables even if the activation records end.

3. Heap allocation is the most flexible allocation scheme as programmers can decide when to
allocate and when to deallocate.

Disadvantages of Heap Storage Allocation

1. Heap allocation is slower as compared to stack allocation.

2. There is a chance of memory leaks (in languages where automatic garbage collection does not
happen) if programmer forgets to deallocate.

3. It can create dangling pointers / references if not handled carefully.

2. Explain various storage allocation strategies with an example ?

Static Storage Allocation

Static storage allocation is the simplest form of memory allocation. In this strategy, the memory for
variables is allocated at compile time. The compiler determines the memory addresses for all variables
before the program starts running, and these addresses remain the same throughout the program’s
execution.

For example, in languages like C or C++, when you declare global variables or static variables, the
memory for these variables is allocated using static storage.

int number = 1; // Global

static int digit = 1;

Advantages of Static Storage Allocation

1. The allocation process is straightforward and easy to understand.


2. The memory is allocated once only at compile time and remains the same throughout the
program completion.

3. Memory allocation is done before the program starts taking memory only on compile time.

Disadvantages of Static Storage Allocation

1. Not highly scalable and offers limited memory flexibility.

2. The size of the data must be known at the compile time.

3. Static storage allocation is not very efficient and there is potential for memory wastage.

Stack Storage Allocation

Stack storage allocation is used for memory allocation of local variables of a function (including main),
where memory is allocated at when the function is called. In this case, memory is assigned in a Last In,
First Out (LIFO) manner, meaning that memory is allocated when a function is called and deallocated
when the function returns.

In C/C++, the local variables of a function are stored in the stack. Each time a function is called, a
new activation record (stack frame) is pushed onto the stack to store the local variables. Once the
function completes, the stack frame is popped off, and the memory used is released.

// when we call the sum function below, memory

// will be allotted for the variables, a, b and ans

void sum(int a, int b){int ans = a+b; cout<<ans;}

Advantages of Stack Storage Allocation

1. Stack allocation provides very fast memory access and management.

2. Memory is automatically managed and deallocated when a function completes.

3. Memory allocation and deallocation is simple (compared to heap) as completely controlled by


compiler.

Disadvantages of Stack Storage Allocation

1. Stack has a limited size which can lead to potential stack overflow errors.

2. It cannot handle dynamic or large memory requirements effectively.

3. The fixed memory size restricts flexibility as programmers have no control about allocation and
deallocation of memory.

Heap Storage Allocation

Heap storage allocation is another form of dynamic memory allocation, but unlike the stack, it allows
memory to be allocated at runtime for variables that can grow or shrink during execution. The heap is
used for memory that is not automatically deallocated when a function call ends. This type of memory
allocation is used when you need to store objects or data that are not tied to a specific function scope.
In C/C++, functions like malloc() or new are used to allocate memory in the heap, and functions like
free() or delete are used to deallocate the memory.

int* ans = new int[5];

Advantages of Heap Storage Allocation

1. Heap allocation is useful when we have data whose size is not fixed and can change during the
run time.

2. We can retain the values of variables even if the activation records end.

3. Heap allocation is the most flexible allocation scheme as programmers can decide when to
allocate and when to deallocate.

Disadvantages of Heap Storage Allocation

1. Heap allocation is slower as compared to stack allocation.

2. There is a chance of memory leaks (in languages where automatic garbage collection does not
happen) if programmer forgets to deallocate.

3. It can create dangling pointers / references if not handled carefully.

3. What are the three forms of Intermediate-code representations? Explain them.

The three common forms of intermediate code representation in compiler design are Postfix Notation,
Syntax Trees, and Three-Address Code.

Postfix Notation

 Also known as reverse Polish notation or suffix notation.

 In the infix notation, the operator is placed between operands, e.g., a + b. Postfix
notation positions the operator at the right end, as in ab +.

 For any postfix expressions e1 and e2 with a binary operator (+) , applying the operator
yields e1e2+.

 Postfix notation eliminates the need for parentheses, as the operator’s position and arity allow
unambiguous expression decoding.

 In postfix notation, the operator consistently follows the operand.

Example 1: The postfix representation of the expression (a + b) * c is : ab + c *


Example 2: The postfix representation of the expression (a – b) * (c + d) + (a – b) is : ab – cd + *ab -+
Read more: Infix to Postfix

Three-Address Code

 A three address statement involves a maximum of three references, consisting of two for
operands and one for the result.

 A sequence of three address statements collectively forms a three address code.


 The typical form of a three address statement is expressed as x = y op z, where x, y,
and z represent memory addresses.

 Each variable (x, y, z) in a three address statement is associated with a specific memory
location.

While a standard three address statement includes three references, there are instances where a
statement may contain fewer than three references, yet it is still categorized as a three address
statement.
Example: The three address code for the expression a + b * c + d : T1 = b * c T2 = a + T1 T3 = T2 + d; T 1 ,
T2 , T3 are temporary variables.

There are 3 ways to represent a Three-Address Code in compiler design:


i) Quadruples
ii) Triples
iii) Indirect Triples
Read more: Three-address code

Syntax Tree

 A syntax tree serves as a condensed representation of a parse tree.

 The operator and keyword nodes present in the parse tree undergo a relocation process to
become part of their respective parent nodes in the syntax tree. the internal nodes are
operators and child nodes are operands.

 Creating a syntax tree involves strategically placing parentheses within the expression. This
technique contributes to a more intuitive representation, making it easier to discern the
sequence in which operands should be processed.

The syntax tree not only condenses the parse tree but also offers an improved visual representation of
the program’s syntactic structure,
Example: x = (a + b * c) / (a – b * c)

You might also like