0% found this document useful (0 votes)
8 views7 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C, explaining its importance for managing memory during program execution. It covers various memory management functions such as malloc, calloc, free, and realloc, along with their differences and common errors. Additionally, it highlights the advantages of linked lists over arrays, emphasizing their dynamic nature and efficiency in memory usage.

Uploaded by

dulalchandra314
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)
8 views7 pages

Dynamic Memory Allocation

The document discusses dynamic memory allocation in C, explaining its importance for managing memory during program execution. It covers various memory management functions such as malloc, calloc, free, and realloc, along with their differences and common errors. Additionally, it highlights the advantages of linked lists over arrays, emphasizing their dynamic nature and efficiency in memory usage.

Uploaded by

dulalchandra314
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/ 7

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.

How is it presented: you must search this in google/youtube. Do it.


Answer no 4:

Dynamic Memory Allocation: Dynamic memory allocation is the process of assigning


the memory space during the execution time or run time. It is the ability for a
program to obtain more memory space at execution time to hold new nodes and to
release space that is no longer needed.
How it helps in building complex programs:

C language requires the number of elements in an array to be specified at compile


time. But we may not be able to do so always. Our initial judgement of size, if it is
wrong, may cause failure of the program or wastage of memory space.
The process of allocating memory at run time is known as dynamic memory
allocation. Although C does not inherently have this facility, there are four librar
routines known as "memory management functions" that can be used for allocating
and freeing memory during program execution. They are listed in table below. These
functions help us build complex application programs that use the available memory
intelligently.

Function and their task

• 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:

Key Differences between malloc() vs calloc()


• malloc() function returns only starting address and does not make it zero, on
the other hand, the calloc() function returns the starting address and makes it
zero.
• In malloc function, the number of arguments is 1, while in calloc function, the
number of arguments is 2.
• malloc() time efficiency is higher than calloc(), whereas malloc() is not secure as
compared to calloc()
• malloc does not initialize memory, whereas calloc performs memory
initialization.
Answer no 6:

(a) *ptr = (int *)malloc(m, sizeof(int));

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.

(b) table = (float *)calloc(100);

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.

(c) node = free(ptr);

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:

Why a Linked List is called a dynamic data structure?

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 :

struct struct name isn’t given.


{ Inside the struct body, semicolon isn’t
char name[30] given after declaring a char array.
struct *next;
};
typedef struct node;
Answer no 10:

typedef struct This header file can be used for linked


{ list data structure. They used typedef to
char name[15]; promote code portability. So that in the
int age; data part of the linked list, the details of
float weight; a person can easily be initialized. Now in
}DATA; program, it is very easy to connect every
struct linked_list node of linked list in our desired
{ position, and operate through these.
DATA person;
struct linked_list *next; N.B : This answer could be lengthy. Here,
}; You can even give an example program
typedef struct linked_list NODE; if you want. I just tried to express the
typedef NODE *NDPTR; gist .

Answer no 11:

int *p; Here, malloc () dynamically allocates the


p = malloc(sizeof(int)); size of an integer and returns a pointer
to the place in memory that is stored in
the pointer variable p.

Answer no 12.

float *p; Here, calloc () dynamically allocates the


p = calloc(10, sizeof(float)); memory. Size of 10 floats. And returns a
pointer to the place in memory that is
stored in the pointer variable p.
Answer no 13.

int i, *ip; Here, output will show


ip = calloc(4, error because of the
sizeof(int)); syntax error in the last
for(i=0; i<4; i++) line.
*ip++ = i*i;
for(i=0; i<4; i++) But if we correct it,
printf("%d\n", *-ip); replace the line with
printf("%d\n", *--ip);
Then, the syntax error will
be removed, and the
output we’ll get is given in
the next column. :)

Answer no 14: 111 will be printed.


Answer no 15:

#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;
}

You might also like