Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Search   Upload Sign In Join

RELATED TITLES
0 0
 Home 4 views

#18 Dynamic Memory Allocation


 Saved
Uploaded by jagriti kumari 
c prog
 Books

    interviewc++ rscpcinit_sapnote1266393
C FAQs
 Audiobooks
Save Embed Share Print

 Magazines
Download 1 of 5    Search document 
 News

 Documents

#18 Dynamic Memory Alloca


 Sheet Music
C Notes Vol-3 by Saurabh Sh

www.mysirg

#18 Dynamic Memory Allocations


By Saurabh Shukla | 2017©mysirg.com

DMA: Dynamic Memory Allocation

To store data we create variables in our program. This can be done by using data types.

In all our previous programs we wrote data type declaration instruction to specify variables t
the compiler. Compiler resolve declaration statements at compile time and maintain variable
related information in symbol table.

The amount of memory allocated for variables depend upon the information created by
compiler at compile time by looking at declaration statements. This is called static memory
allocation.

The benefit of static memory allocation is its


its cost effectiveness in terms of time.
time. Since decis
about amount of memory needed for variables are already done at compile time, no extra ti
would be consumed for this purpose at the time of execution

The major disadvantage of static memory allocation is lack of flexibility in memory consump
Since the decision regarding amount of memory has already been taken at compile time no
change could be made during run time.

There are scenarios where programmer is not sure about the amount of data to be handled
the program. Static memory allocation is definitely a bad choice in such case. Solution to this
problem is dynamic memory allocation.

Decision of allocating memory to store data has taken at run time gives flexibility to the

programmer to make program efficient and cost effective Sign
inup to vote
terms of on this titleconsumption
memory
 Useful  Not useful
Allocating memory at run time can be done by using either of two predefined functions:
  Upload Sign In Join

 Home

 Saved

 Books

 Audiobooks

 Magazines

 News

 Documents

 Sheet Music
Search   Upload Sign In Join

RELATED TITLES
0 0
 Home 4 views

#18 Dynamic Memory Allocation


 Saved
Uploaded by jagriti kumari 
c prog
 Books

    interviewc++ rscpcinit_sapnote1266393
C FAQs
 Audiobooks
Save Embed Share Print

 Magazines
Download 1 of 5    Search document 
 News

 Documents

#18 Dynamic Memory Alloca


 Sheet Music
C Notes Vol-3 by Saurabh Sh

www.mysirg

Function malloc()

The malloc() function allocates a block of memory in bytes. The malloc() function is like a
request to the RAM of the system to allocate memory, if the request is granted, returns a
pointer to the first block. However if it fails to al locate memory returns NULL.

The malloc() function reserves a block of memory of specified size and returns a pointer of ty
void.

ptr=(cast type*)malloc(byte size);

Function calloc()

The calloc() function is used for requesting memory space at run time for storing derived dat
types such as arrays and structures. While malloc() allocates a single block of storage space,
calloc() allocates multiple blocks of storage, each of same size, and then sets all bytes to zero

ptr = (cast type*)calloc(n, element size);


You're Reading a Preview
This statement allocates contiguous space
Unlock for n with
full access blocks each
a free trial.of size element size bytes. All by
are initialized to zero and pointer to the first byte of the allocated region is returned. If not
enough space NULL is returned Download With Free Trial

Function free()

The free function is used to de-allocate the previously allocated memory using malloc or call
functions.

free(ptr);

Sign up to vote on this title
Simple linked list program to understand the usage of DMA:
 Useful  Not useful
Write a program to prepare a list of integers and manage basic operations on it like adding
new data to the list and deleting old data from the list.
  Upload Sign In Join

 Home

 Saved

 Books

 Audiobooks

 Magazines

 News

 Documents

 Sheet Music
Search   Upload Sign In Join

RELATED TITLES
0 0
 Home 4 views

#18 Dynamic Memory Allocation


 Saved
Uploaded by jagriti kumari 
c prog
 Books

    interviewc++ rscpcinit_sapnote1266393
C FAQs
 Audiobooks
Save Embed Share Print

 Magazines
Download 1 of 5    Search document 
 News

 Documents

#18 Dynamic Memory Alloca


 Sheet Music
C Notes Vol-3 by Saurabh Sh

www.mysirg

void viewlist();
void deletedata();
int main()
{
int ch;
while(1)
{
clrscr();
printf(“ \n1. Add data to list”);
printf(“ \n2. View list”);
printf(“ \n3. Delete data from list”);
printf(“ \n4. Exit”);
printf(“ \n\nEnter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
adddata();
break;
case 2:
You're Reading a Preview
viewlist();
break;
Unlock full access with a free trial.
case 3:
deletedata();
Download With Free Trial
break;
case 4:
exit(0);
default:
printf(“Invalid Choice”);
}
getch();
} 
return(0); Sign up to vote on this title
}  Useful  Not useful
void adddata()
{
  Upload Sign In Join

 Home

 Saved

 Books

 Audiobooks

 Magazines

 News

 Documents

 Sheet Music
Search   Upload Sign In Join

RELATED TITLES
0 0
 Home 4 views

#18 Dynamic Memory Allocation


 Saved
Uploaded by jagriti kumari 
c prog
 Books

    interviewc++ rscpcinit_sapnote1266393
C FAQs
 Audiobooks
Save Embed Share Print

 Magazines
Download 1 of 5    Search document 
 News

 Documents

#18 Dynamic Memory Alloca


 Sheet Music
C Notes Vol-3 by Saurabh Sh

www.mysirg

t=t->p;
t->p=n;
}
}
void viewlist()
{
struct node *t;
if(START==NULL)
printf(“List is empty”);
else
{
t=START;
while(t!=NULL)
{
printf(“ %d “,t ->x);
t=t->p;
}
}
}
void deletedata()
You're Reading a Preview
{
struct node *r;
Unlock full access with a free trial.
r=START;
if(START==NULL)
Download With Free Trial
printf(“List is empty”);
else
{
START=r->p;
free(r);
}
}
Program to input string of any length 
Sign up to vote on this title
int main()  Useful  Not useful
{
char ch,*p,*temp=0;
  Upload Sign In Join

 Home

 Saved

 Books

 Audiobooks

 Magazines

 News

 Documents

 Sheet Music
Search   Upload Sign In Join

RELATED TITLES
0 0
 Home 4 views

#18 Dynamic Memory Allocation


 Saved
Uploaded by jagriti kumari 
c prog
 Books

    interviewc++ rscpcinit_sapnote1266393
C FAQs
 Audiobooks
Save Embed Share Print

 Magazines
Download 1 of 5    Search document 
 News

 Documents

#18 Dynamic Memory Alloca


 Sheet Music
C Notes Vol-3 by Saurabh Sh

www.mysirg

printf("%c",ch);
*(p+i)=ch;
free(temp);
temp=(char*)calloc(size+2,sizeof(char));
for(i=0;i<=size;i++)
{
*(temp+i)=*(p+i);
}
*(temp+i)='\0';
free(p);
size++;
p=(char*)calloc(size+1,sizeof(char));
i=0;
while(*(temp+i)!='\0')
{
*(p+i)=*(temp+i);
i++;
}

}
You're Reading a Preview
printf("You haveUnlock
entered: %s",temp);
full access with a free trial.
printf("\nLength of string is %d",strlen(temp));
getch(); Download With Free Trial
return(0);
}

References:

YouTube video links

Lecture 18 Dynamic Memory Allocation in C part-1 



Sign up to vote on this title
o  Useful  Not useful
https://youtu.be/ZSzzY3adfDM?list=PL7ersPsTyYt2Q-SqZxTA1D-melSfqBRM
 Lecture 18 Dynamic Memory Allocation in C part-2
o https://youtu.be/nd7WJ4Q9gog?list=PL7ersPsTyYt2Q-SqZxTA1D-melSfqBRM
  Upload Sign In Join

 Home

 Saved

 Books

 Audiobooks

 Magazines

 News

 Documents

 Sheet Music

You might also like