Professional Documents
Culture Documents
18 Dynamic Memory Allocation
18 Dynamic Memory Allocation
RELATED TITLES
0 0
Home 4 views
interviewc++ rscpcinit_sapnote1266393
C FAQs
Audiobooks
Save Embed Share Print
Magazines
Download 1 of 5 Search document
News
Documents
www.mysirg
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 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
interviewc++ rscpcinit_sapnote1266393
C FAQs
Audiobooks
Save Embed Share Print
Magazines
Download 1 of 5 Search document
News
Documents
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.
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
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
interviewc++ rscpcinit_sapnote1266393
C FAQs
Audiobooks
Save Embed Share Print
Magazines
Download 1 of 5 Search document
News
Documents
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
interviewc++ rscpcinit_sapnote1266393
C FAQs
Audiobooks
Save Embed Share Print
Magazines
Download 1 of 5 Search document
News
Documents
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
interviewc++ rscpcinit_sapnote1266393
C FAQs
Audiobooks
Save Embed Share Print
Magazines
Download 1 of 5 Search document
News
Documents
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:
Home
Saved
Books
Audiobooks
Magazines
News
Documents
Sheet Music