馃攳
Basics of Dynamic Memory Allocation - YouTube
Channel: Neso Academy
[0]
Here in this presentation, we will discuss
[2]
some of the basics of
dynamic memory allocation.
[4]
So, let's get started.
[5]
First we will discuss about
static memory allocation.
[8]
Then we will move to dynamic
memory allocation.
[11]
Memory allocated during compile
time is called static memory.
[15]
It should be very clear that
the memory allocated
[17]
during compilation time, or the time
when the programmer is creating
[20]
the program is called static memory.
[22]
And the memory allocated is
fixed and cannot be
[24]
increased or decreased during run time.
[26]
If you run the program,
obviously the memory, which has
[29]
been allocated at compile time, is fixed and
[31]
cannot be increased or decreased.
[34]
For example, here You can see
that we have an array,
[36]
which consists of five elements.
[38]
Although you can see over here
[40]
that the size is fixed.
[41]
So, memory is allocated at compile time.
[44]
and obviously, here the size is fixed.
[46]
The programmer has fixed this size,
[48]
and we cannot increase this
size or decrease the size at runtime.
[52]
It should be very clear.
[53]
Now, there are certain problems which
[55]
we faced in static memory allocation.
[57]
If you are allocating memory
for an array during compile time,
[60]
then you have to fix the
size at the time of declaration.
[62]
Obviously, there is least
flexibility because you have to
then you have to fix the
size at the time of declaration.
[62]
Obviously, there is least
flexibility because you have to
[65]
fix the size at the time
of declaration itself.
[68]
Size is fixed and user cannot
increase or decrease
[71]
the size of the array at run time.
[73]
This size is fixed. User cannot
increase or decrease the size
[78]
of the array, or any object at run time.
[81]
If the value stored by the user
in the array at run time is less
[84]
than the size specified, then
there will be wastage of memory.
[88]
Let me tell you that, here
I'm talking about array.
[90]
It could be any object, right?
[91]
So, if the value stored by the
user in the array at run time is
[94]
less than the size specified,
if the user enter elements
[97]
less than the size specified, then
[99]
obviously there will be a wastage of memory
[101]
If the value stored by the
user in the array at run time
[105]
is more than the size specified.
[106]
If he or she enters more
elements in the array,
[109]
then obviously the program
may crash or misbehave.
[112]
This is also a possibility, right?
[114]
Now, here comes someone to rescue.
[117]
The one who can solve the problem
is dynamic memory allocation.
[121]
The process of allocating
memory at the time of execution
[124]
is called dynamic memory allocation.
[126]
It should be very clear that
the process of allocating
[129]
memory at the time of execution
is called dynamic memory allocation.
[133]
You are allocating memory at run time.
[135]
So, user has the flexibility.
[137]
He or she can allocate memory
[139]
according to his or her needs, right?
[141]
You might have remembered this diagram
[143]
from C programming lectures.
[145]
This is nothing but a memory
layout of C program.
[147]
These are the different memory segments.
[149]
The one which we will talk about here
[151]
is the heap memory segment.
[153]
Heap is the segment of memory,
[154]
where dynamic memory allocation takes place.
[157]
Unlike stack, where memory is allocated
[159]
or deallocated in a defined order,
[162]
heap is an area of memory where memory is allocated
[165]
or deallocated without any order or randomly.
[168]
In stack, everything is defined in order.
[170]
You have to enter elements at
[172]
the top of the stack, you have to delete elements
[175]
from the top of the stack.
[176]
Everything is well defined and
everything is in the order.
[179]
So, memory is allocated or deallocated
[182]
in a defined order in case of stack.
[184]
But in case of heap, there
is no restriction as such.
[187]
Memory is allocated or deallocated
without any order or randomly.
[192]
There are certain built-in
functions that can help
[195]
in allocating or deallocating
some memory space at run time.
[199]
It is interesting because, you know,
[201]
there are certain built-in functions available
[203]
that can help us in allocating
[204]
or deallocating some memory
space at run time in heap.
[207]
Before revealing those
functions, I would like to
[209]
discuss one very important point.
[212]
Pointers play a very important
role in dynamic memory allocation.
[215]
And this is one such
application of pointers.
[218]
Allocated memory can only be
accessed through pointers.
[222]
The only possible way which we can access
[224]
he memory allocated
dynamically is through pointers.
[227]
Now, here are certain built-in
functions that I was talking about.
[230]
malloc(), calloc(), realloc(), and free().
[233]
These are the built-in functions which
[235]
are used to allocate or
deallocate memory dynamically.
[238]
We will discuss all these functions
[240]
in the subsequent lectures.
[242]
Okay friends.
[243]
This is it for now.
[244]
Thank you for watching this presentation.
Most Recent Videos:
You can go back to the homepage right here: Homepage





