The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+12 votes

A language $L$ allows declaration of arrays whose sizes are not known during compilation. It is required to make efficient use of memory. Which one of the following is true?

  1. A compiler using static memory allocation can be written for $L$
  2. A compiler cannot be written for $L$; an interpreter must be used
  3. A compiler using dynamic memory allocation can be written for $L$
  4. None of the above
asked in Compiler Design by Veteran (59.8k points)
edited by | 1.1k views

1 Answer

+19 votes
Best answer


Using dynamic memory allocation, memory will be allocated to array at runtime.

answered by Boss (19.9k points)
edited by
@Arjun And allocation will be done in heap memory or stack memory ??
During runtime, it is difficult to do anything on stack- as activation record size is decided at compile time. So, it must always be on heap. But I don't know if any language does this on stack.
If activation record size is known at compile time. Then why can't we place it in the area where static or global variables is placed ?? why do we need heap for it ??
Or you want to say there may be need of some dynamic allocation inside the activation record. So we allocate it to heap area.
Static and global memory area is compile time determined and this is part of the binary. At run time, we cannot change this. Only memory area freely available for the process to change during runtime is heap.
Then how static area and stack area differs ?? Does stack area not freely available during run time ??
Activation records are created on stack. These are created and destroyed during program run- that is stack area changes its size during program run. But static memory area remains constant.

Even though memory on stack area is created during run time- the amount of memory (activation record size) is determined at compile time. For static area, the memory is determined during compile time and this will be present as soon as program is loaded.  

This is with respect to a simple programming language like C.
@arjun  sir plz verify is it   the diffrence b/w heap area and stack area ??    the memory allocated  in the stack area is based on the amount memory required for activaction record size and this will be done at compile time . and that's why  here is limitation to  allocate the memory on stack area bcz its totally  depend on what memory assign for activaction record size at compile time  . but in case of  heap  we also allocate the memory on run time but here freely we allocate without any limitation .
Yes, but stack is a runtime entity. At compile time compiler only reserves the stack space for activation record. This gets used (allocated on actual memory) only during program run. Only DATA segment part of the program like static variables, string literals etc. are allocated during compile time. For heap area, how much memory to be allocated is also determined at run time.
on static memory allocation is used for  global variable and allocate during compile time and never gona be change during run time. and for stack we determine the memory  require of activation record at compile time and this will be allocate during run time and for heap memory is determines  and  allocate at run time only
then how does it handle recursion???

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,126 questions
53,251 answers
70,502 users