You are on page 1of 5

EXPERIMENT NUMBER–3.

Name: Shreyank Pushkar


Branch: CSE (Blockchain)
Class UID: 21CBT1118
Section: ON21BDA+BCD+BCN+BCG-125
Semester: 1

AIM OF THE EXPERIMENT – LEARN HOW TO USE DYNAMIC MEMORY


ALLOCATION IN C

PREREQUISITES- BASIC KNOWLEDGE OF C CONSTRUCTS

Program 10.1: 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

1. We have to enter the no. of max. characters we want to input.


2. We have to enter the name of string where e.g, we input “Hello, How are you”.
3. Then, there comes the output which returns the pointer type of void.

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


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);

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


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)

There were no errors encountered while program’s execution.

PROGRAMS’ EXPLANATION (in brief)

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


malloc() function is used for allocating block of memory at runtime. This
function reserves a block of memory of the given size and returns
a pointer of type void. This means that we can assign it to any type of
pointer using typecasting. If it fails to allocate enough space as specified,
it returns a NULL pointer.

OUTPUT

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


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

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101

You might also like