Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Memory Management: Heap, Stack, and Dynamic Allocation, Slides of Computer Fundamentals

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

2011/2012

Uploaded on 07/31/2012

karthik
karthik ๐Ÿ‡ฎ๐Ÿ‡ณ

4.6

(16)

95 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Heap๎˜ƒvs.๎˜ƒStack
โ€ขWhen๎˜ƒa๎˜ƒprogram๎˜ƒis๎˜ƒloaded๎˜ƒinto๎˜ƒmemory,๎˜ƒit๎˜ƒis๎˜ƒ
organized๎˜ƒinto๎˜ƒthree๎˜ƒareas๎˜ƒof๎˜ƒmemory,๎˜ƒcalled๎˜ƒ
segments:๎˜ƒ
โ€“text๎˜ƒsegment๎˜ƒ(code๎˜ƒsegment)
โ€“stack๎˜ƒsegment
โ€“heap๎˜ƒsegment.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C Memory Management: Heap, Stack, and Dynamic Allocation and more Slides Computer Fundamentals in PDF only on Docsity!

Heap

vs.

Stack

-^ When

a^ program

is^

loaded

into

memory,

it^

is

organized

into

three

areas

of

memory,

called

segments:^ โ€“^

text

segment

(code

segment)

-^ stack

segment

-^ heap

segment.

Heap

vs.

Stack

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

โ€ข^ NULL

Pointer

is^ a

special

pointer

with

all^

zero

values.

-^ we

cannot

store

and

read

value

from

it.

-^ If^

a^ pointer

holds

NULL,

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

calloc

(^1000

,^ sizeof

(^ int

)^ )

(^ int

*****^

)^ calloc

(^1000

,^ sizeof

(^ int

)^ )

;

void

*^ calloc

(^ long

n^

,^ long

el

โ€size

)^ ;

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

NULL.

-^ 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

]^

;

Example

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++

;

free

(^ iPtr

)^ ;

realloc(void

*iPtr,

size_t

size);

iPtr

=^ (^

int^

*****^ )^ calloc

(^1000

,^ sizeof

(^ int

)^ )^

Conclude

โ€โ€^ DMA

Dynamic

Memory

Allocation

a.^

Cost

in^ terms

of^

management:

Free

the

memory

Change

the

size

of^

a^ pre

โ€allocated

memory

chunk

1.^

It^ allocates

the

next

consecutive

memory

if^ available

2.^

Or,^

looks

for

a^ new

bigger

chunk

some

where

else

3.^

iptr

still

points

to^

the

new

chunk

of^

the

memory

4.^

The

values

from

will

be^

copied

to^

the

new

chunk

5.^

The

older

chunk

will

be^

automatically

returned

to^

the

heap

Example

main

(^ )

{ funct

(^ )^

} funct

(^ )

{ int

*iPtr

iPtr

=^ malloc

(^1000

*^

(^ sizeof

(^ int

)^ )^

)^ ;

//^ used

the

memory

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

)^ ;

//ptr

points

to^

an^

invalid

location