









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of heap and stack memory segments in c programming, explaining the differences between local and global variables, function parameters, and static variables. It also covers dynamic memory allocation using pointers, void pointers, and functions like calloc and malloc, discussing the importance of checking for null pointers and handling memory leaks.
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!
-^ When
-^ stack
-^ heap
Stack
Heap
โ^ local variables are put on the stack
โ^ unless
they are also declared as 'staticโ โ^ function
parameters
are
allocated
on
the
stack โ^ local variables that are declared on the stackare not automatically initialized by the systemso they usually have garbage in them until youset them โ^ variables on the stack disappear when thefunction
exits (thus, if a function is called multiple
times,
it's
local
variables
and
parameters are recreated and destroyed eachtime the function is called end exited).
โ^ global variables are on the heap โ^ static local variables are on the heap (this ishow they keep their value between functioncalls) โ^ memory allocated by new, malloc and callocare on the heap โ^ The
heap
segment
provides
more
stable
storage
of^
data
for
a^
program;
memory
allocated in the heap remains in existence forthe duration of a program. โThe memory allocated in the heap area, ifinitialized to zero at program start, remainszero until the program makes use of it. Thus,the heap area need not contain garbage.
NULL
Dynamic
Memory
Allocation^ contd..
is^ a
value
defined
in^ stdlib.h
Pointer
is^ a
special
pointer
with
all^
zero
values.
-^ we
cannot
store
and
read
value
from
it.
-^ If^
a^ pointer
holds
it^ doesnโt
point
to^
a^ valid
memory
location
calloc
(
n
,
m
)
;
Space
in^ terms
of
numbers
of^ elements
Space
in^ terms
of^ size
each
of^ elements
The^
memory
returned
is^ a
one
block
No^ gaps.
We
store
its^ starting
address
in^ a^
pointer
(^ int
*****^
)^ calloc
(^1000
,^ sizeof
(^ int
)^ )
;
Dynamic
Memory
Allocation^ contd..
-^ What
happens
if^ either
we
ask
for
too
much
memory
or^
the
memory
is^ not
available
in^ a
single
chunk.
-^ The
call
to^
calloc();
will
fail
and
it^ will
return
-^ So
check
if^ the
returned
memory
is^ NULL
before
using
the
pointer
void
malloc
(^
n^
)^ ;
Number
of^ bytes required
Dynamic
Memory
Allocation^ contd..
malloc
(
*sizeof
(^
int
)^
)^ )
;
malloc
(^
n^
(^ sizeof
(^
float
)^
)^ )
;
It^ returns
a^
void
pointer
to
a^
chunk
of
memory which is allocated from the heap ifthat memory is available. Otherwise return anull pointer.
Static
Memory
Allocation
#define
MAXSTUDENT
100
int
Student
[^
MAXSTUDENT
]^
;
int numstd ;int *iPtr ,
*sPtr ; printf("Enter the
number
of^ students \nโ
)^ ;
scanf(
โ%dโ,&numstd ); iPtr =
malloc (
numstd *
(^ sizeof (
int )
)^ )^ ;
if^ (^ iPtr ==
NULL
)
{^
printf("Error on
malloc \nโ)
;
return 1
; /*^ Use
a^ nonzero
return
to^ indicate
an^ error
has^
occurred
*/
} //^ a^ while
loop
to^ read
the^ ages
of^ the
student
and^
place
them
in^ the
memory
sPtr =
iPtr ; sPtr++
;
realloc(void
*iPtr,
size_t
size);
iPtr
int^
*****^ )^ calloc
,^ sizeof
(^ int
Conclude
Dynamic
Memory
Allocation
a.^
Cost
in^ terms
of^
management:
Free
the
memory
Change
the
size
of^
a^ pre
โallocated
memory
chunk
It^ allocates
the
next
consecutive
memory
if^ available
Or,^
looks
for
a^ new
bigger
chunk
some
where
else
iptr
still
points
to^
the
new
chunk
of^
the
memory
The
values
from
will
be^
copied
to^
the
new
chunk
The
older
chunk
will
be^
automatically
returned
to^
the
heap
A good practice: The function where weallocate the memory, we should free it inthe same function.
Conclusion^ โโ
Dangling
Pointers
If^ due
to^
some
reason,
the
memory
has
been
de^
allocated.
but
pointer
still
has
the
starting
address.
What
will
happen
if^ we
try^
to^ write
some
thing
using
this
pointer.
int
*ptr
,^ *ptr
;
ptr
=^
malloc
(
(^ sizeof
(^ int
)^ )
)^ ;
ptr
=^
ptr
;
โ โ โ free^
(^ ptr
)^ ;