0% found this document useful (0 votes)
11 views3 pages

DPPL Sessional-II Solution

The document contains solutions to questions related to C programming and memory management concepts. It discusses how compiler computes addresses of multi-dimensional arrays at compile time using row-major layout. It also summarizes issues related to dangling references and memory leaks in code snippets. The solutions explain use of tombstones technique to prevent dangling references and bookkeeping information used in heap management with implicit free list.
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)
11 views3 pages

DPPL Sessional-II Solution

The document contains solutions to questions related to C programming and memory management concepts. It discusses how compiler computes addresses of multi-dimensional arrays at compile time using row-major layout. It also summarizes issues related to dangling references and memory leaks in code snippets. The solutions explain use of tombstones technique to prevent dangling references and bookkeeping information used in heap management with implicit free list.
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/ 3

DPPL Sessional-II Solution

Jan-May 2024
CSE Sem-IV
Q.1 You have been given the following information in “C” language and lower bound of the given array
is assumed to be 0.

int A[3][4][5] = {{0,1,2,3,7}, {1,2,23,4,33}, {2,3,24,5,56}, {8, 9, 11, 22,78}


{3,4,5,6,9}, {4,5,6,7,2}, {5,6,7,8,5},{87, 45, 56, 67, 88}
{2,4,3,5,9}, {1,2,4,9,7}, {2,4,5,8,7} {8, 5, 6, 7, 9}}
Assume that array starts at a base address 2000 (consider that the address is in decimal) and each
integer takes 4 bytes. Find out the addresses of A [0, 3, 3] and A [1, 2, 2]. Elaborate as to how does
compiler compute these addresses at compile time? Consider the row-major layout.

Elaborate as to how does compiler compute these addresses at compile time -2M

Given the general 3-D array A. : array [L1..U1] of array [L2..U2] of array [L3..U3] of element;
Compiler computes the following components, namely S1, S2 and S3 based on the constants D1, D2
and D3 defined below.
D1 = U1-L1+1
D2 = U2-L2+1
D3 = U3-L3+1
Let
S3 = size of element
S2 = D3 * S3
S1 = D2 * S2
where Size of a row (S2) is the size of an individual element (S3) times the number of elements in a
row (assuming the row-major order) and Size of a plane (S1) is the size of row (S2) times the
number of rows in a plane.
The above details are computed by the Compiler using the logical addresses. It also passes the
symbol table to the runtime system. The symbol table will have (L1 * S1) + (L2 * S2) + (L3 * S3)
compile-time constant. Also, S1, S2 and S3 are kept in the symbol table entry of A.

Address Computations-2M
int A[3][4][5]
S3= 4,
S2= (5-0) * S3 = 20,
S1= (4-0)*S2= 80
The addresses of A[0, 3, 3] and A[1 2 2] are then computed as follows.

A[0, 3, 3] = A + (0-0) *80 + (3-0) *20 + (3-0) *4 = A +72


So address of A[0 3 3] = 2000+ 72 = 2072

A[1 2 2] = A + (1-0) *80 + (2-0) *20 + (2-0) *4 = A +128


So address of A[1 2 2] = 2000+128 = 2128

Q.2 a. Consider the following code snippets in C++. Is there a problem of dangling reference and/or
memory leakage in case of (i) and (ii)?
(i). (ii).
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() { int main() {
int *ptr = new int; int *ptr = new int;
int * ptr2; int * ptr2 =new int;
*ptr = 5; *ptr = 5;
ptr2=ptr; *ptr2 = 10;
delete ptr; ptr2=ptr;
return 0; delete ptr;
} return 0;
}

b. Discuss the solution to the problem of dangling references using the technique that prevents the
Dangling reference from accessing memory location (Note: this technique uses 2-level indirection).

(i)
In code snippet (i) there is a problem of dangling reference, since ptr and ptr2 both ponting to freed
memory location.
In code snippet (ii)there is a problem of memory leak asno pointer is pointing to memory location
which contain 10 and it is not freed. The code snippet also has a problem of dangling reference,
since ptr and ptr2 both ponting to freed memory location.

(ii)
To solve dangling reference problem using 2-level indirection, one can use Tombstone method.

Tombstones:

 Allow a language implementation cancatch all dangling references to objects inboth stack
and heap.
 An extra level of indirection
 A pointer holds an address of thetombstone and the tombstone contains theaddress of the
object.
 When an object is reclaimed, thetombstone is modified to contain a value (zero) that can
notbe a valid address.
 It may be allocated from the heap or from a separate pool of memory.

Figure Tombstones. A valid pointer refers to a tombstone that in turn refers to an object. A dangling
reference refers to an “expired” tombstone
Q. 3. Answer the following with respect to the Heap management using an implicit list of all blocks.

(i) How do you differentiate between the allocated block and free block?
The status bit is kept in the bookkeeping information. If the value of this bit is 0, then the block is
free else if the value of this bit is 1, then the block is allocated.

(ii) Assume that we have heap blocks that are multiple of 16 bytes and alignment is also 16 bytes.
How much memory is required for the bookkeeping information considering 64-bit words if both
allocation status and block size are kept together? In this case, how would you know the size of the
block while reading it?
Only one word (64-bit) is needed for the bookkeeping information. Within the same word, we will
keep allocation status and the block size as the last four bits in this case will be all 0 and a least
significant bit is used to differentiate between allocated block and free block.
If the block is free, then the size of the block can be read as it is.
If the block is allocated, the while reading the size of the block, the masking of the least significant
bit needs to be done.
For. Ex. 16-byte aligned sizes have 4 zeroes in low-order bits.
00000000
00010000
00100000
00110000

If a block starting with address of 00010000 is allocated, then we need to keep it 00010001. And
while reading the size mask the LSB by ANDing with 0 to get 00010000.

(iii) How much memory is required for the bookkeeping information considering 64-bit words if
both allocation status and block size are kept differently? In this case, will there be a space
overhead?
Two words (each 64-bit) are needed for the bookkeeping information. In one word, we will keep
allocation status and in the other word, the block size will be kept. In this case, block sizes can be of
a multiple of one, two, etc.
Here there is a space overhead as the no. of words is 2 per block to be kept in the bookkeeping info.

(iv). What is the purpose of optional padding in the block format?

32/1 8 8 8

An optional padding is given for alignment purpose. Suppose a block of 32 words is allocated. Then
First word goes for bookkeeping information and 2nd and 3rd words go for actual payload (data) and
last word is kept blank called as padding for the alignment, e.g. in the above example, green colored
block (8 bytes) shows the padding. In this case it is there for 16 bytes alignment. It may or may not
be there depending on the memory required for the payload, therefore it is optional.

You might also like