Q1.Define memory allocation and explain its type:
Ans.
Memory allocation is a process by which a particular computer program is allocated memory
space. The memory allocation is done either before or at the time of program execution.
• There are two types of memory allocations in C language namely, static (at
compiletime) and dynamic (run-dme).
• When the allocation of memory performs at the compile-time, then it is known as static
memory allocation.
• Array is a data structure where elements are stored in consecutive memory locations. In
order to occupy the consecutive memory space, block of memory that is required for the
array should be allocated before it use.
• Once memory is located it cannot be changed any more. This is why array is known as
static data structure.
• When the memory allocation is done at the execution or run-time, then it is called
dynamic memory allocation.
• The linked list is called dynamic data structure where amount of memory' required can
be changed during its use.
• Linked list can grow or shrink during the execution of a program. In C language, memory
can be allocated dynamically by calling malloc(), calloc() or realloc() functions.
Q2.Write down difference between static and dynamic memory allocation:
Static memory allocation Dynamic memory allocation
1.In static memory allocation the memory is 1. In dynamic memory allocation the memory
allocated at the compile time. is allocated at the run-time.
2. Static memory allocation is done before 2. Dynamic memory allocation is done during
program execution (i.e., during compile- program execution.
time).
3. Static memory allocation allots memory 3.Dynamic memory allocation allots memory
from the stack. from the heap.
4. It is less emcient and execution of the
4.It is more efficient and execution is slow.
program is fast.
5.In static memory allocation once the 5. The memory can be allotted at any time in
memory is allotted, it Will remain from the program (user can allocate more memorj,
beginning to end of the program. when required as well as release the memory)
, 6. In static memory allocation the memory is 6. In dynamic memory allocation the memory
allocated automatically by the compiler when is allocated only when there is an explicit call
the definition of statements is encountered. to malloc() and calloc() functions
7.Static memory allocation is preferred/used in 7. Dynamic memory allocation is
an array. preferred/used in the linked list
8.In static memory allocation, the allocated 8. In dynamic memory allocation, executing a
memory cannot be changed while executing a program, the allocated memory can be
Program. changed.
Q3.Define linkedlist and list its advantages and disadvantages:
Ans.
• Linkedlist is a collection of n number of data which is implemented using the concept of
dynamic memory allocation.
• Linkedlist provides us the facility to stored as many as elements we want to stored.
• This enable us to create memory during run time which is also known as concept of
dynamic memory allocation.
• Types:singlelinkedlist,doublylinkedlist,circularlinkedlist
• Declaration:
Struct node
{
Int data;
Struct node *link;
}*start=NULL;
• Representation:
Ans.
Memory allocation is a process by which a particular computer program is allocated memory
space. The memory allocation is done either before or at the time of program execution.
• There are two types of memory allocations in C language namely, static (at
compiletime) and dynamic (run-dme).
• When the allocation of memory performs at the compile-time, then it is known as static
memory allocation.
• Array is a data structure where elements are stored in consecutive memory locations. In
order to occupy the consecutive memory space, block of memory that is required for the
array should be allocated before it use.
• Once memory is located it cannot be changed any more. This is why array is known as
static data structure.
• When the memory allocation is done at the execution or run-time, then it is called
dynamic memory allocation.
• The linked list is called dynamic data structure where amount of memory' required can
be changed during its use.
• Linked list can grow or shrink during the execution of a program. In C language, memory
can be allocated dynamically by calling malloc(), calloc() or realloc() functions.
Q2.Write down difference between static and dynamic memory allocation:
Static memory allocation Dynamic memory allocation
1.In static memory allocation the memory is 1. In dynamic memory allocation the memory
allocated at the compile time. is allocated at the run-time.
2. Static memory allocation is done before 2. Dynamic memory allocation is done during
program execution (i.e., during compile- program execution.
time).
3. Static memory allocation allots memory 3.Dynamic memory allocation allots memory
from the stack. from the heap.
4. It is less emcient and execution of the
4.It is more efficient and execution is slow.
program is fast.
5.In static memory allocation once the 5. The memory can be allotted at any time in
memory is allotted, it Will remain from the program (user can allocate more memorj,
beginning to end of the program. when required as well as release the memory)
, 6. In static memory allocation the memory is 6. In dynamic memory allocation the memory
allocated automatically by the compiler when is allocated only when there is an explicit call
the definition of statements is encountered. to malloc() and calloc() functions
7.Static memory allocation is preferred/used in 7. Dynamic memory allocation is
an array. preferred/used in the linked list
8.In static memory allocation, the allocated 8. In dynamic memory allocation, executing a
memory cannot be changed while executing a program, the allocated memory can be
Program. changed.
Q3.Define linkedlist and list its advantages and disadvantages:
Ans.
• Linkedlist is a collection of n number of data which is implemented using the concept of
dynamic memory allocation.
• Linkedlist provides us the facility to stored as many as elements we want to stored.
• This enable us to create memory during run time which is also known as concept of
dynamic memory allocation.
• Types:singlelinkedlist,doublylinkedlist,circularlinkedlist
• Declaration:
Struct node
{
Int data;
Struct node *link;
}*start=NULL;
• Representation: