Dynamic Memory Allocation
Dynamic Memory Allocation
[N.B : Linked List part is not in our 1-2 syllabus. Read that
part only if you got enough time.]
Answer no 1:
a) True.
b) False.
c) False. You have to free each element of that array specifically.
d) True.
e) True.
f) True.
g) True.
Answer no 2:
a) calloc()/malloc()
b) Linked List
c) Self-referential structure
d) NULL pointer
e) An ordered collection of items where the addition of new items and the
removal of existing items always takes place at the same end.
Answer no 3:
Linked List: A linked list is a linear data structure consisting of a group of nodes
where each node points to the next node by using a pointer.
• malloc : Allocates request size of bytes and returns a pointer to the first byte of
the allocated space.
• calloc : Allocates space for an array of elements, initializes them to zero and
then returns a pointer to the memory.
• free : Frees previously allocated space.
• realloc: Modifies the size of previously allocated space.
Answer no 5:
Error : First of all, this line doesn’t tell us on what type of pointer we are storing the
newly allocated memory.
Secondly, inside the malloc() bodypart, the syntax used here is the syntax that is used
in calloc() where it takes two arguments. But malloc() function takes 1 argument only.
So that also will show an error.
Error : Here inside the calloc() bodypart, only one argument is used. But we know that
in calloc() function, the number of argument is two. The number of things to be
allocated and the size of each thing (in bytes) . Therefore, an error will show on the
console.
Error : free() function in C doesn’t return any value. It has a return type of void.
Therefore, cannot be assigned into any variable.
Answer no 7:
A linked list is called a dynamic list because it can be used with a data collection that
grows and shrinks during program execution. The exact amount of memory space
required by a program depends on the data being processed and so this requirement
cannot be found in advance. Linked list uses runtime allocation of memory resulting
in no wastage of memory space.
The Advantages of using Linked List over Arrays:
1. Dynamic Data Structure: Linked List being a dynamic data structure can shrink
and grow at the runtime by deallocating or allocating memory, so there is no
need for an initial size in linked list. Whereas an initial size has to be declared
in an array, and the number of elements cannot exceed that size.
2. No Memory Wastage: As the size of a linked list can grow or shrink at runtime,
so there is no memory wastage. Only the required memory is allocated. In
arrays, we have to first initialize it with a size which we may or may not fully
use; hence wastage of memory may occur.
3. Implementation: Some very helpful data structures like queues and stacks can
be easily implemented using a Linked List.
4. Insertion and Deletion Operation: In a Linked List, insertion and deletion
operations are quite easy, as there is no need to shift every element after
insertion or deletion. Only the address present in the pointers needs to be
updated. While in an array, we have to shift elements. Suppose we have an
array that is sorted, and now we need to insert some element in the array in a
sorted way. Let arr[]= [ 1, 3 , 5, 7, ….. ], and we have to insert 2. So, all the
elements after 1 have to move by one place towards the right.
Answer no 8 : Just see the last portion of page no 428. Trust me, ami olosh na hoile
eitao likhe ditam.
Answer no 9 :
Answer no 11:
Answer no 12.
#include<stdio.h> Output:
#include<stdlib.h>
int main()
{
struct node
{
int m;
struct node *next;
}x, y, z, *p;
x.m = 10;
y.m = 20;
z.m = 30;
x.next = &y;
y.next = &z;
z.next = NULL;
p = x.next;
while(p!=NULL)
{
printf("%d\n", p->m);
p = p->next;
return 0;
}