1. Differentiate call by
value and call by reference. Or Differentiate pass by value and
pass by reference. (Gujarat University 2014, 2015, 2016)
Difference between call by
value and call by reference in c
|
No.
|
Call by value
|
Call by reference
|
|
1
|
A copy of value is passed
to the function
|
An address of value is
passed to the function
|
|
2
|
Changes made inside the
function is not reflected on other functions
|
Changes made inside the
function is reflected outside the function also
|
|
3
|
Actual and formal arguments
will be created in different memory location
|
Actual and formal arguments
will be created in same memory location
|
|
4
|
Ex.,
Add(a, b);
|
Ex.,
Add(&a, &b);
|
|
|
|
|
2. Explain the function
with prototype, used to change the memory size which is already allocated. (Gujarat
University 2014)
Please refer program list.
3. Explain any one
function to allocate memory dynamically with syntax. (Gujarat University 2014)
Please refer program list.
4. Explain how to access
character array using pointer. (Gujarat University 2014)
Please refer program list.
5. Show the use of
realloc(), calloc(), free(), malloc(). Differentiate malloc() and calloc().
(Gujarat University 2014, 2015, 2016)
DYNAMIC MEMORY ALLOCATION
IN C:
The process of allocating
memory during program execution is called dynamic memory allocation.
C language offers 4 dynamic
memory allocation functions. They are,
1. malloc()
2. calloc()
3. realloc()
4. free()
|
Function
|
Syntax
|
|
malloc ()
|
malloc (number
*sizeof(int));
|
|
calloc ()
|
calloc (number,
sizeof(int));
|
|
realloc ()
|
realloc (pointer_name,
number * sizeof(int));
|
|
free ()
|
free (pointer_name);
|
1. MALLOC() FUNCTION IN C:
·
malloc ()
function is used to allocate space in memory during the execution of the
program.
·
malloc () does
not initialize the memory allocated during execution. It carries garbage
value.
·
malloc ()
function returns null pointer if it couldn’t able to allocate requested amount
of memory.
2.
CALLOC() FUNCTION IN C:
·
calloc ()
function is also like malloc () function. But calloc () initializes the
allocated memory to zero. But, malloc() doesn’t.
3.
REALLOC() FUNCTION IN C:
·
realloc ()
function modifies the allocated memory size by malloc () and calloc ()
functions to new size.
·
If enough space
doesn’t exist in memory of current block to extend, new block is allocated for
the full size of reallocation, then copies the existing data to new block and
then frees the old block.
4.
FREE() FUNCTION IN C:
·
free () function
frees the allocated memory by malloc (), calloc (), realloc () functions and
returns the memory to the system.
Difference
between Static Memory Allocation and Dynamic Memory Allocation:
|
Static
memory allocation
|
Dynamic
memory allocation
|
|
In
static memory allocation, memory is allocated while writing the C program.
Actually, user requested memory will be allocated at compile time.
|
In
dynamic memory allocation, memory is allocated while executing the program.
That means at run time.
|
|
Memory
size can’t be modified while execution.
Example: array |
Memory
size can be modified while execution.
Example: Linked list |
Difference
Between malloc() and calloc():
|
malloc()
|
calloc()
|
|
It
allocates only single block of requested memory
|
It
allocates multiple blocks of requested memory
|
|
int
*ptr;ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory
only allocated in one block.
Total = 80 bytes |
int
*ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory
will be created and each contains 20*4 bytes of memory.
Total = 1600 bytes |
|
malloc
() doesn’t initializes the allocated memory. It contains garbage values
|
calloc
() initializes the allocated memory to zero
|
|
type
cast must be done since this function returns void pointer int *ptr;ptr =
(int*)malloc(sizeof(int)*20 );
|
Same
as malloc () function int *ptr;ptr = (int*)calloc( 20, 20 * sizeof(int) );
|
6. Explain the concept of
the Linked List. Explain different type of Linked List. (Gujarat University
2014, 2015, 2016)
(Note: Kindly refer class
diagram for detail explain)
1. It is a data Structure which consists if group of
nodes that forms a sequence.
2. It is very common data structure that is
used to create tree, graph and other abstract data types.
Linked list comprise of group
or list of nodes in which each node have link to next node to form a chain.
1. Linked List is Series of Nodes
2. Each node Consist of two Parts viz Data Part
& Pointer Part
3. Pointer Part stores the address of the next
node
Linked list is created
using following elements –
|
No
|
Element
|
Explanation
|
|
1
|
Node
|
Linked list is collection
of number of nodes
|
|
2
|
Address Field in Node
|
Address field in node is
used to keep address of next node
|
|
3
|
Data Field in Node
|
Data field in node is used
to hold data inside linked list.
|
Linked List Advantages:
1. Linked List is Dynamic data Structure .
2. Linked List can grow and shrink during run time.
3. Insertion and Deletion Operations are Easier
4. Efficient Memory Utilization, i.e no need to
pre-allocate memory
5. Faster Access time, can be expanded in constant
time without memory overhead
6. Linear Data Structures such as Stack,Queue can
be easily implemeted using Linked list
Linked List Disadvantages:
1.
Wastage of Memory: Pointer
Requires extra memory for storage. Suppose we want to store 3 integer data
items then we have to allocate memory –
3 Integer * Size
= 3 * 2 bytes
= 6 bytes
2. No Random Access: In array we can access nth element easily just by using a[n]. In
Linked list no random access is given to user, we have to access each
node sequentially.
3 . Time Complexity: Array can be randomly accessed , while the Linked list cannot be
accessed Randomly. Individual nodes are not stored in the contiguous
memory Locations.
4. Reverse Traversing is difficult: In case if we are using singly linked list then it
is very difficult to traverse linked list from end. If using doubly linked
list then though it becomes easier to traverse from end but still it
increases again storage space for back pointer.
5. Heap Space Restriction: Whenever memory is
dynamically allocated , It utilizes memory from heap. Memory is allocated
to Linked List at run time if and only if there is space available in
heap. If there is insufficient space in heap then it won’t create any
memory.
7. Differentiate Singly
and Doubly Linked Lists with an example. (Gujarat University 2014)
|
No.
|
Singly Linked List
|
Doubly Linked List
|
|
1
|
Singly linked list allows
you to go one way direction
|
Doubly linked list has two
way directions next and previous
|
|
2
|
Singly linked list uses
less memory per node (one pointer)
|
Doubly linked list uses
More memory per node than Singly Linked list (two pointers)
|
|
5
|
If we need to save memory
in need to update node values frequently and searching is not required, we
can use Singly Linked list.
|
If we need faster
performance in searching and memory is not a limitation we use Doubly Linked
List
|
|
7
|
If we know in advance that
element to be searched is found near the end of the list(for example name 'BCA'
in a telephone directory), even then singly linked list is traversed
sequentially from beginning.
|
In doubly linked list If we
know in advance that element to be searched is found near the end of the
list(for example name 'BCA' in a telephone directory), then the list can
traversed from the end thereby saving time
|
|
8
|
In single list Each node
contains at least two parts:
a) info/value/data b) link/next/address |
In doubly linked list Each
node contains at least three parts:
a) info/value/data b) link/address to next node c) link/address to previous node |
8. What is dynamic memory
allocation? How it is differs from the static memory allocation? (Gujarat
University 2014)
Please Refer Question No. 5
9. Write a function to
insert the value at the end of the linked list. (Gujarat University 2014)
10. Write a function to
delete the value from the linked list. (Gujarat University 2014)
11. How one pointer points
to another pointer? (Gujarat University 2015)
12. List different
operations on Linked List. How one pointer points to another pointer? (Gujarat
University 2015, 2016)
Please refer program list.
Sr. No. 1 to 4.
13. Differentiate linked
list and array. How one pointer points to another pointer? (Gujarat University
2015, 2016)
|
|
Array
|
Linked List
|
|
Define
|
Array is a collection of elements having same data type
with common name.
|
Linked list is an ordered collection of elements which are
connected by links/pointers.
|
|
Access
|
In array, elements can be accessed using index/subscript
value, i.e. elements can be randomly accessed like arr[0], arr[3], etc. So
array provides fast and random access.
|
In linked list, elements can’t be accessed randomly but
can be accessed only sequentially and accessing element
takes 0(n) time.
|
|
Memory Structure
|
In array, elements are stored in consecutive manner
in memory.
|
In linked list, elements can be stored at any available
place as address of node is stored in previous node.
|
|
Insertion & Deletion
|
Insertion & deletion takes more time in array as
elements are stored in consecutive memory locations.
|
Insertion & deletion are fast & easy in linked
list as only value of pointer is needed to change.
|
|
Memory Allocation
|
In array, memory is allocated at compile time i.e. Static
Memory Allocation.
|
In linked list, memory is allocated at run time i.e. Dynamic
Memory Allocation.
|
|
Types
|
Array can be single dimensional, two dimension
or multidimensional.
|
Linked list can be singly, doubly or circular linked
list.
|
|
Dependency
|
In array, each element is independent, no connection with
previous element or with its location.
|
In Linked list, location or address of elements is stored
in the link part of previous element/node.
|
|
Extra Space
|
In array, no pointers are used like linked list so no need
of extra space in memory for pointer.
|
In linked list, adjacency between the elements are
maintained using pointers or links, so pointers are used and for that extra
memory space is needed.
|
No comments:
Post a Comment