0% found this document useful (0 votes)
29 views5 pages

Experiment Number 3.3: Aim of The Experiment

This document describes an experiment on dynamic memory allocation in C. The aim is to learn how to use malloc to store a character string in a block of memory and then reallocate memory to store a larger string. The program code demonstrates allocating memory using malloc, storing a string input by the user, reallocating memory to a larger size, and storing a new string. The learning outcomes are understanding dynamic memory behavior using pointers, developing modular programs using control structures, and debugging programs in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Experiment Number 3.3: Aim of The Experiment

This document describes an experiment on dynamic memory allocation in C. The aim is to learn how to use malloc to store a character string in a block of memory and then reallocate memory to store a larger string. The program code demonstrates allocating memory using malloc, storing a string input by the user, reallocating memory to a larger size, and storing a new string. The learning outcomes are understanding dynamic memory behavior using pointers, developing modular programs using control structures, and debugging programs in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NUMBER 3.

AIM OF THE EXPERIMENT: –


Learn how to use
dyanamic memory allocation in c.

PROGRAM TO BE DONE: -
WAP to store a character string in block
of memory space created by malloc and then modify the same to
store a large string.

FLOWCHART/ ALGORITHM: -
● firstly, we will define header file
● then we include main body.
● Then we define the data types.
● Then we use scanf and printf function to take input and
statement output respectively.
● Then we use malloc function and puts function to allocate
the number of bytes in memory and to show the output
respectively.
● Then we use reallocate function and for loop .
● Atlast, we print the output using printf function.
PROGRAM CODE: -
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i;
printf("\tRUNTIME MEMORY ALLOCATION\n");
printf("Enter max number of characters you want to input\n");
scanf("%d",&n);
char *p = (char*)malloc(n*sizeof(char));
if(p==NULL)
{
printf("Memory allocation fails..");
exit(0);
}
puts("Enter string please");
for(i=0;i<n;i++)
{
scanf("%c",p+i);
}
//Now terminate string with NULL character
*(p+i)= '\0';
printf("You wrote %s",p);
fflush(stdin);
printf("\nEnter new size\n");
scanf("%d",&n);
p= realloc(p,n*sizeof(char));
puts("\nEnter new string please");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%c",p+i);
}
//Now terminate string with NULL character
*(p+i)= '\0';
printf("You wrote %s\n",p);
free(p);
return 0;
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)
Error encountered due to the syntax error

● PROGRAMS’ EXPLANATION (in brief) :-


C language provides
features to manual management of memory, by using this feature
we can manage memory at run time, whenever we require memory
allocation or reallocation at run time by using Dynamic Memory
Allocation functions we can create amount of required
memory.malloc is used to allocate specified number of bytes
(memory blocks) calloc - It is used to allocate specified number of
bytes (memory blocks) and initialize all memory with 0.

● reaclloc - It is used to reallocate the dynamically


allocated memory to increase or decrease amount of the
memory.
● free - It is used to release dynamically allocated memory.

OUTPUT
LEARNING OUTCOMES

● Remember the concepts related to fundamentals of C language, draw flowcharts and


write algorithm/pseudocode.

● Understand the way of execution and debug programs in C language.

● Apply various constructs, loops, functions to solve mathematical and scientific problem.

● Analyze the dynamic behavior of memory by the use of pointers.

● Design and develop modular programs for real world problems using control structure
and selection structure.

EVALUATION COLUMN (To be filled by concerned faculty only)


Sr. No. Parameters Maximum Marks
Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post-Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre-Lab Questions
4. Total Marks 20

You might also like