SlideShare a Scribd company logo
Department of CSE
1
2.3 DYNAMIC MEMORY
ALLOCATION
Objectives
• Learn how to allocate and free memory, and to control dynamic
arrays of any type of data in general and structures in particular.
• Practice and train with dynamic memory in the world of work
oriented applications.
• To know about the pointer arithmetic
• How to create and use array of pointers.
2 Department of CSE
Agenda
• Dynamic memory allocation
• Malloc
• Calloc
• Realloc
• free
• PointerArithmetic
• Array of pointers
3 Department of CSE
Introduction
• While doing programming, if you are aware about the size of an
array, then it is easy and you can define it as an array.
• For example to store a name of any person, it can go max 100
characters so you can define something as follows:
• char name[100]
• But now let us consider a situation where you have no idea about
the length of the text you need to store, for example you want to
store a detailed description about a topic. Here we need to define
a pointer to character without defining how much memory is
required and later based on requirement we can allocate memory .
4 Department of CSE
MemoryAllocation Function
5 Department of CSE
Difference between Static and Dynamic
memory allocation
S.no
Static memory
allocation
Dynamic memory
allocation
1
In static memory allocation, memory
is allocated while writing the C
program.Actually, user requested
memory will be allocated at compile
time.
In dynamic memory allocation, memory
is allocated while executing the program.
That means at run time.
2
Memory size can’t be modified while
execution.
Memory size can be modified while
execution.
Example: array Example: Linked list
6 Department of CSE
Introduction
• Creating and maintaining dynamic structures requires dynamic
memory allocation— the ability for a program to obtain more
memory space at execution time to hold new values, and to release space no
longer needed.
7 Department of CSE
MemoryAllocation Functions
8 Department of CSE
Syntax
• The following are the function used for dynamic memory allocation
• void *malloc(int num);
• This function allocates an array of num bytes and leave them
uninitialized.
• void *calloc(int num, int size);
• This function allocates an array of num elements each of which
size in bytes will be size.
• void *realloc(void *address, int newsize);
• This function re-allocates memory extending it upto newsize.
• void free(void *address);
• This function releases a block of memory block specified by
address.
9 Department of CSE
Block MemoryAllocation (malloc)
• Malloc function allocates a block of memory that contains the
number of bytes specified in its parameter.
• It returns a void pointer to the first byte of the allocated memory
• The allocated memory is not initialized . We should therefore assume
that it will contain unknown values and initialize it as required by our
program.
• The function declaration is as follows
• void* malloc (size_t size)
• If it is not successful malloc return NULL pointer.
• An attempt to allocate memory from heap when memory is
insufficient is known as overflow.
10 Department of CSE
malloc
11 Department of CSE
• It is up to the program to check the memory overflow
• If it doesn't the program produces invalid results or aborts with
an invalid address the first time the pointer is used.
malloc
• Malloc function has one more potential error.
• If we call malloc function with zero size , the results are
unpredictable.
• It may return a NULL pointer
• Never call malloc with a zero size!!!!!
12 Department of CSE
Contiguous MemoryAllocation (calloc)
• Calloc is primarily used to allocate memory for arrays.
• It differs from malloc only in that it sets memory to null characters.
• void *calloc (size_t element-count,size_t element-size)
13 Department of CSE
Reallocation of memory(realloc)
• The realloc function can be highly inefficient and should be used
advisedly.
• When given a pointer to the previously allocated block of memory,
realloc changes the size of the block by deleting or extending the
memory at the end of the block.
• If memory cannot be extended because of other allocations, realloc
allocates a completely new block and copies the existing memory
allocation to new allocation, and deletes the old allocation.
• void *realloc (void* ptr,size_t newSize)
14 Department of CSE
realloc
15 Department of CSE
Releasing Memory (free)
• When memory locations allocated by malloc, calloc or realloc are no
longer needed, they should be freed using the predefined function
free.
• void free(void* ptr)
• Below shows the example where first one releases a
single element allocated with malloc
• Second example shows 200 elements were allocated
with calloc . When free the pointer 200 elements are
returned to the heap.
16 Department of CSE
free
17 Department of CSE
Difference between malloc and calloc
S.no malloc() calloc()
1
It allocates only single block of requested
memory
It allocates multiple blocks of requested memory
2
int *ptr;ptr = malloc( 20 * sizeof(int) );For
the above, 20*4 bytes of memory only
allocated in one block.
int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For
the above, 20 blocks of memory will be created
and each contains 20*4 bytes of memory.
Total = 80 bytes Total = 1600 bytes
3
malloc () doesn’t initializes the allocated
memory. It contains garbage values
calloc () initializes the allocated memory to zero
4
type cast must be done since this function
returns void pointer int *ptr;ptr =
(int*)malloc(sizeof(int)*20 );
Same as malloc () function int *ptr;ptr =
(int*)calloc( 20, 20 * sizeof(int) );
18 Department of CSE
Resizing and Releasing Memory
• When your program comes out, operating system automatically
release all the memory allocated by your program but as a good
practice when you are not in need of memory anymore then you
should release that memory by calling the function free().
• Alternatively, you can increase or decrease the size of an allocated
memory block by calling the function realloc().
19 Department of CSE
Example-1
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
20 Department of CSE
Enter number of elements: 3
Enter elements of array: 2 7 1
Sum=10
Dynamic-ex1.c
Example - 2
21 Department of CSE
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Enter number of elements: 3
Enter elements of array: 2 1 3
Sum=6
Dynamic-ex2.c
Example - 3
22 Department of CSE
#include <stdio.h>
#include <stdlib.h>
int main(){
int *ptr,i,n1,n2;
printf("Enter size of array: ");
scanf("%d",&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf("Address of previously allocated memory: ");
for(i=0;i<n1;++i)
printf("%ut",ptr+i);
printf("nEnter new size of array: ");
scanf("%d",&n2);
ptr=realloc(ptr,n2);
for(i=0;i<n2;++i)
printf("%ut",ptr+i);
return 0;
}
Enter size of array: 3
Address of previously allocated memory: 7474944
7474948 7474952
Enter new size of array: 5
7474944 7474948 7474952 7474956 7474960
Dynamic-ex3.c
Example - 4
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
char *description;
strcpy(name,"Zara Ali");
/* allocate memory dynamically */
description = malloc( 200 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr,"Error - unable to allocate required memoryn");
}
else
{
strcpy( description,"Zara ali a DPS student in class 10th");
}
printf("Name = %sn", name );
printf("Description: %sn",description );
}
23 Department of CSE
Name = Zara Ali
Description:Zara ali a DPS student in class 10th
Dynamic-ex4.c
Example - 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
char *description;
strcpy(name,"Zara Ali");
/* allocate memory dynamically */
description = malloc( 30 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr,"Error - unable to allocate required memoryn");
}
else
{
strcpy( description,"Zara ali a DPS student.");
}
24 Department of CSE
Example – 5 Cont---
/* suppose you want to store bigger description */
description = realloc( description,100 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memoryn");
}
else
{
strcat( description, "She is in class 10th");
}
printf("Name = %sn", name );
printf("Description: %sn", description );
/* release memory using free() function */
free(description);
}
25 Department of CSE
Name = Zara Ali
Description: Zara ali a DPS student.She is in class 10th
Dynamic-ex5.c
Memory Leaks
• A memory leak occurs when allocated memory is never used again
but is not freed.
• I can happen when
• The memory’s address is lost
• The free function is never invoked though it should be
• The problem with memory leak is that the memory cannot be
reclaimed and use later. The amount of memory available t the heap
manager will be decreased.
• If the memory is repeatedly allocated and then lost, then the
program may terminate when more memory is needed but malloc
cannot allocate it because it ran out of memory.
26 Department of CSE
Example
char *chunk;
while(10
{
chunk=(char*) malloc (1000000);
printf(“allocatingn”);
}
• The variable chunk is assigned memory from heap. However this
memory is not freed before another block of memory is assigned to
it.
• Eventfully the application will run out of memory and terminate
abnormally.
27 Department of CSE
Department of CSE
28
POINTERARITHMETIC
PointerArithmetic
• This section introduces the concept of pointer arithmetic, and this
will form one of the very important building blocks in understanding
the functionality of pointers.
29 Department of CSE
Pointer Expressions and PointerArithmetic
• Arithmetic operations can be performed on pointers
• Increment/decrement pointer (++ or --)
• Add an integer to a pointer( + or += , - or -=)
• Pointers may be subtracted from each other
• All these operations meaningless unless performed on an array
• NOTE: Division and Multiplication are not allowed.
PointerArithmetic
• int a,b,*p,*q
• p=-q /* illegal use of pointers*/
• p<<=1 /* illegal use of pointers*/
• p=p-b /*valid*/
• p=p-q /* nonportable pointer conversion*/
• p=(int*) p-q /*valid*/
• p=p-q-a /*valid*/
• p=p+a /*valid*/
• p=p+q /* invalid pointer addition*/
• p=p*q /* illegal use of pointers*/
• p=p/q /* illegal use of pointers*/
• p=p/a /* illegal use of pointers*/
31 Department of CSE
Pointer Increment – Example - 1
#include<stdio.h>
void main()
{
int n;
int *pn;
pn=&n;
int *pn1;
pn1=pn+1;
printf("%d %dn", pn,pn1);
double d;
double *pd;
pd=&d;
double *pd1;
pd1=pd+1;
printf("%d %dn", pd,pd1);
}
32 Department of CSE
2686788 2686792
2686768 2686776
Arithmetic-ex1.c
Incrementing Pointer :
• Incrementing Pointer is generally used in array because we have contiguous
memory in array and we know the contents of next memory location.
• Incrementing PointerVariable Depends Upon data type of the Pointer variable
33 Department of CSE
Example – 2
#include<stdio.h>
int main()
{
int *ptr=(int *)1000;
printf(“OldValue of ptr : %u",ptr);
ptr=ptr+1;
printf("NewValue of ptr : %u",ptr);
return 0;
}
34 Department of CSE
OldValue of ptr : 1000
NewValue of ptr : 1004
Arithmetic-ex2.c
Difference between two integer Pointers –
Example - 3
#include<stdio.h>
int main(){
float *ptr1=(float *)1000;
float *ptr2=(float *)2000;
printf("nDifference : %dn",ptr2-ptr1);
return 0;
}
35 Department of CSE
Difference : 250
Arithmetic-ex3.c
Explanation
• Ptr1 and Ptr2 are two pointers which holds memory address of Float
Variable.
• Ptr2-Ptr1 will gives us number of floating point numbers that can be
stored.
• ptr2 - ptr1 = (2000 - 1000) / sizeof(float)
• = 1000 / 4
36 Department of CSE
Pointer Division – Example - 4
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
ptr1 = (int *)1000;
ptr2 = ptr1/4;
return(0);
}
37 Department of CSE
Illegal Use of operator : INVALID
Arithmetic-ex4.c
Pointer Expressions and PointerArithmetic-Arrays
• 5 element int array on machine with 4 byte ints
• vPtr points to first element v[ 0 ]
• at location 3000 (vPtr = 3000)
• vPtr += 2; sets vPtr to 3008
• vPtr points to v[ 2 ] (incremented by 2), but the machine has
4 byte ints, so it points to address 3008
Array v and a pointer variable vPtr that points to v.
The pointer vPtr after pointer arithmetic
Pointer Expressions and PointerArithmetic
• Subtracting pointers
• Returns number of elements from one to the other. If
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
• vPtr2 - vPtr would produce 2
• Pointer comparison ( <, == , > )
• See which pointer points to the higher numbered array element
• Also, see if a pointer points to 0
The Relationship Between Pointers and Arrays
• Arrays and pointers closely related
• Array name like a constant pointer
• Pointers can do array subscripting operations
• Define an array b[ 5 ] and a pointer bPtr
• To set them equal to one another use:
bPtr = b;
• The array name (b) is actually the address of first element of the
array b[ 5 ]
bPtr = &b[ 0 ]
• Explicitly assigns bPtr to address of first element of b
The Relationship Between Pointers and Arrays
• Element b[ 3 ]
• Can be accessed by *( bPtr + 3 )
• Where n is the offset. Called pointer/offset notation
• Can be accessed by bptr[ 3 ]
• Called pointer/subscript notation
• bPtr[ 3 ] same as b[ 3 ]
• Can be accessed by performing pointer arithmetic on the array
itself
*( b + 3 )
Example - 5
1
2 Using subscripting and pointer notations with arrays */
3
4 #include <stdio.h>
5
6 int main( void )
7 {
8 int b[] = { 10, 20, 30, 40 }; /* initialize array b */
9 int *bPtr = b; /* set bPtr to point to array b */
10 int i; /* counter */
11 int offset; /* counter */
12
13 /* output array b using array subscript notation */
14 printf( "Array b printed with:nArray subscript notationn" );
15
16 /* loop through array b */
17 for ( i = 0; i < 4; i++ ) {
18 printf( "b[ %d ] = %dn", i, b[ i ] );
19 } /* end for */
20
21 /* output array b using array name and pointer/offset notation */
22 printf( "nPointer/offset notation wheren"
23 "the pointer is the array namen" );
24
25 /* loop through array b */
26 for ( offset = 0; offset < 4; offset++ ) {
27 printf( "*( b + %d ) = %dn", offset, *( b + offset ) );
28 } /* end for */
29
Array subscript notation
Pointer/offset notation
30 /* output array b using bPtr and array subscript notation */
31 printf( "nPointer subscript notationn" );
32
33 /* loop through array b */
34 for ( i = 0; i < 4; i++ ) {
35 printf( "bPtr[ %d ] = %dn", i, bPtr[ i ] );
36 } /* end for */
37
38 /* output array b using bPtr and pointer/offset notation */
39 printf( "nPointer/offset notationn" );
40
41 /* loop through array b */
42 for ( offset = 0; offset < 4; offset++ ) {
43 printf( "*( bPtr + %d ) = %dn", offset, *( bPtr + offset ) );
44 } /* end for */
45
46 return 0; /* indicates successful termination */
47
48 } /* end main */
Array b printed with:
Array subscript notation
b[ 0 ] = 10
b[ 1 ] = 20
b[ 2 ] = 30
b[ 3 ] = 40
(continued on next slide… )
Pointer subscript notation
Pointer offset notation
Arithmetic-ex5.c
(continued from previous slide…)
Pointer/offset notation where
the pointer is the array name
*( b + 0 ) = 10
*( b + 1 ) = 20
*( b + 2 ) = 30
*( b + 3 ) = 40
Pointer subscript notation
bPtr[ 0 ] = 10
bPtr[ 1 ] = 20
bPtr[ 2 ] = 30
bPtr[ 3 ] = 40
Pointer/offset notation
*( bPtr + 0 ) = 10
*( bPtr + 1 ) = 20
*( bPtr + 2 ) = 30
*( bPtr + 3 ) = 40
1
2 Copying a string using array notation and pointer notation. */
3 #include <stdio.h>
4
5 void copy1( char * const s1, const char * const s2 ); /* prototype */
6 void copy2( char *s1, const char *s2 ); /* prototype */
7
8 int main( void )
9 {
10 char string1[ 10 ]; /* create array string1 */
11 char *string2 = "Hello"; /* create a pointer to a string */
12 char string3[ 10 ]; /* create array string3 */
13 char string4[] = "Good Bye"; /* create a pointer to a string */
14
15 copy1( string1, string2 );
16 printf( "string1 = %sn", string1 );
17
18 copy2( string3, string4 );
19 printf( "string3 = %sn", string3 );
20
21 return 0; /* indicates successful termination */
22
23 } /* end main */
24
25 /* copy s2 to s1 using array notation */
26 void copy1( char * const s1, const char * const s2 )
27 {
28 int i; /* counter */
29
30 /* loop through strings */
31 for ( i = 0; ( s1[ i ] = s2[ i ] ) != '0'; i++ ) {
32 ; /* do nothing in body */
33 } /* end for */
34
35 } /* end function copy1 */
36
37 /* copy s2 to s1 using pointer notation */
38 void copy2( char *s1, const char *s2 )
39 {
40 /* loop through strings */
41 for ( ; ( *s1 = *s2 ) != '0'; s1++, s2++ ) {
42 ; /* do nothing in body */
43 } /* end for */
44
45 } /* end function copy2 */
string1 = Hello
string3 = Good Bye
Condition of for loop
actually performs an action
Department of CSE
48
ARRAY OF POINTERS
Introduction
• An array of pointer is similar to an array of ant predefined data type
• As a pointer variable always contain an address, an array of pointer is
a collection of addresses.
• These can be address of ordinary isolated variable or of array
elements.
• The elements of an array of pointers are stored in the memory just
like elements of any other kind of array.
• Example is given below..
Example - 1
#include <stdio.h>
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr[MAX]; //array of pointers
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %dn", i, *ptr[i] );
}
return 0;
}
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
Arraypointer-ex1.c
Example - 2
#include<stdio.h>
void main()
{
int arr[3]={1,2,3};
int i, *ptr[3];
for(i=0;i<3;i++)
ptr[i]=arr+i;
for(i=0;i<3;i++)
printf("%p %dn", ptr[i],*ptr[i]);
}
51 Department of CSE
0028FF30 1
0028FF34 2
0028FF38 3
Arraypointer-ex2.c
Arrays of Pointers - Strings
• Arrays can contain pointers
• For example: an array of strings
char *suit[ 4 ] = { "Hearts", "Diamonds",
"Clubs", "Spades" };
• Strings are pointers to the first character
• char * – each element of suit is a pointer to a char
• The strings are not actually stored in the array suit, only pointers to
the strings are stored
• suit array has a fixed size, but strings can be of any size
Case study: Roman numeral equivalents – Example
- 3
#include <stdio.h>
void main( void ) {
int decimal_number = 101, a = 0, b = 0;
const char *x[11] = {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc", "c"};
const char *y[10] = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"};
while ((decimal_number > 100) || (decimal_number < 0)) {
printf("Enter the decimal numbers in the range 1 to 100:n");
scanf("%d", &decimal_number);
}
a = decimal_number/10;
b = decimal_number%10;
printf("The equivalent roman is %s%sn", x[a], y[b]);
}
Enter the decimal numbers in the range 1 to 100:
15
The equivalent roman is xv
Arraypointer-ex3.c
Review
• Dynamic Memory allocation
• PointerArithmetic
• Array of pointers
Try itYourself
• Write a program using dynamic memory allocation to get a student’s mark and
display it back.
• Write a program to do the following
The process for finding a prime is quite simple. First, you know by inspection that 2,
3, and 5 are the first three prime numbers, because they aren’t divisible by
anything other than 1 and themselves. Because all the other prime numbers must
be odd (otherwise they would be divisible by 2), you can work out the next
number to check by starting at the last prime you have and adding 2. When
you’ve checked out that number, you add another 2 to get the next to be
checked, and so on. to check whether a number is actually prime rather than just
odd, you could divide by all the odd numbers less than the number that you’re
checking, but you don’t need to do as much work as that. if a number is not prime,
it must be divisible by one of the primes lower than the number you’re checking.
Because you’ll obtain the primes in sequence, it will be sufficient to check a
candidate by testing whether any of the primes you’ve already found is an exact
divisor. Store all primes in an array using pointers and pointer arithmetic.
55 Department of CSE
Try itYourself
• Find Largest Element Using Dynamic Memory Allocation and
pointer arithmetic to access the array
• Write a C program to find the sum of two 1D matrices using
dynamic memory and pointer arithmetic.
56 Department of CSE
Try itYourself –Ans - Student mark
#include<stdio.h>
#include<stdlib.h>
void main()
{
int no, *pt,i;
clrscr();
printf("Enter no of Students :");
scanf("%d",&no);
pt=(int *)malloc(no*2);
if(pt== NULL)
{
printf("nnMemory allocation failed!");
exit(0);
}
57 Department of CSE
Student mark
printf("* * * * Enter roll no of students. * * * *n");
for (i=0;i<no;i++)
{
printf("-->");
scanf("%d",(pt+i));
}
printf("n* * * * Entered roll no. * * * *n");
for (i=0;i<no;i++)
{
printf("%d, ",*(pt+i));
}
}
58 Department of CSE

More Related Content

Similar to Programming in C language specially allocation (20)

PPTX
C dynamic ppt
RJ Mehul Gadhiya
 
DOCX
Dma
Acad
 
PPT
Dynamic Memory Allocation
vaani pathak
 
PPTX
Dynamic memory allocation
Mohammad Usman
 
PPT
Dynamic allocation
CGC Technical campus,Mohali
 
PDF
dynamic-allocation.pdf
ngonidzashemutsipa
 
PPSX
4 dynamic memory allocation
Frijo Francis
 
PPTX
dynamicmemoryallocation.pptx
Niharika606186
 
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
PPTX
Structures_and_Files[1] - Read-Only.pptx
Ramesh Bssv
 
PPT
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
PDF
Data Structure - Dynamic Memory Allocation
babuk110
 
PPTX
Dynamic memory allocation
UTTAM VERMA
 
PPTX
Dynamic memory Allocation in c language
kiran Patel
 
PDF
Dynamic memory allocation
Gem WeBlog
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPTX
Dynamic Memory Allocation.pptx
ssuser688516
 
PPTX
Memory Management.pptx
BilalImran17
 
PPT
Bullet pts Dyn Mem Alloc.pptghftfhtfytftyft
2023209914aditya
 
PPTX
PF UE LEC 7 Pointers programming fundamentals (2).pptx
helpme43
 
C dynamic ppt
RJ Mehul Gadhiya
 
Dma
Acad
 
Dynamic Memory Allocation
vaani pathak
 
Dynamic memory allocation
Mohammad Usman
 
Dynamic allocation
CGC Technical campus,Mohali
 
dynamic-allocation.pdf
ngonidzashemutsipa
 
4 dynamic memory allocation
Frijo Francis
 
dynamicmemoryallocation.pptx
Niharika606186
 
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
TriggeredZulkar
 
Structures_and_Files[1] - Read-Only.pptx
Ramesh Bssv
 
CLanguage_ClassPPT_3110003_unit 9Material.ppt
NikeshaPatel1
 
Data Structure - Dynamic Memory Allocation
babuk110
 
Dynamic memory allocation
UTTAM VERMA
 
Dynamic memory Allocation in c language
kiran Patel
 
Dynamic memory allocation
Gem WeBlog
 
Dynamic memory allocation in c
lavanya marichamy
 
Dynamic Memory Allocation.pptx
ssuser688516
 
Memory Management.pptx
BilalImran17
 
Bullet pts Dyn Mem Alloc.pptghftfhtfytftyft
2023209914aditya
 
PF UE LEC 7 Pointers programming fundamentals (2).pptx
helpme43
 

Recently uploaded (20)

PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Day2 B2 Best.pptx
helenjenefa1
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Ad

Programming in C language specially allocation

  • 1. Department of CSE 1 2.3 DYNAMIC MEMORY ALLOCATION
  • 2. Objectives • Learn how to allocate and free memory, and to control dynamic arrays of any type of data in general and structures in particular. • Practice and train with dynamic memory in the world of work oriented applications. • To know about the pointer arithmetic • How to create and use array of pointers. 2 Department of CSE
  • 3. Agenda • Dynamic memory allocation • Malloc • Calloc • Realloc • free • PointerArithmetic • Array of pointers 3 Department of CSE
  • 4. Introduction • While doing programming, if you are aware about the size of an array, then it is easy and you can define it as an array. • For example to store a name of any person, it can go max 100 characters so you can define something as follows: • char name[100] • But now let us consider a situation where you have no idea about the length of the text you need to store, for example you want to store a detailed description about a topic. Here we need to define a pointer to character without defining how much memory is required and later based on requirement we can allocate memory . 4 Department of CSE
  • 6. Difference between Static and Dynamic memory allocation S.no Static memory allocation Dynamic memory allocation 1 In static memory allocation, memory is allocated while writing the C program.Actually, user requested memory will be allocated at compile time. In dynamic memory allocation, memory is allocated while executing the program. That means at run time. 2 Memory size can’t be modified while execution. Memory size can be modified while execution. Example: array Example: Linked list 6 Department of CSE
  • 7. Introduction • Creating and maintaining dynamic structures requires dynamic memory allocation— the ability for a program to obtain more memory space at execution time to hold new values, and to release space no longer needed. 7 Department of CSE
  • 9. Syntax • The following are the function used for dynamic memory allocation • void *malloc(int num); • This function allocates an array of num bytes and leave them uninitialized. • void *calloc(int num, int size); • This function allocates an array of num elements each of which size in bytes will be size. • void *realloc(void *address, int newsize); • This function re-allocates memory extending it upto newsize. • void free(void *address); • This function releases a block of memory block specified by address. 9 Department of CSE
  • 10. Block MemoryAllocation (malloc) • Malloc function allocates a block of memory that contains the number of bytes specified in its parameter. • It returns a void pointer to the first byte of the allocated memory • The allocated memory is not initialized . We should therefore assume that it will contain unknown values and initialize it as required by our program. • The function declaration is as follows • void* malloc (size_t size) • If it is not successful malloc return NULL pointer. • An attempt to allocate memory from heap when memory is insufficient is known as overflow. 10 Department of CSE
  • 11. malloc 11 Department of CSE • It is up to the program to check the memory overflow • If it doesn't the program produces invalid results or aborts with an invalid address the first time the pointer is used.
  • 12. malloc • Malloc function has one more potential error. • If we call malloc function with zero size , the results are unpredictable. • It may return a NULL pointer • Never call malloc with a zero size!!!!! 12 Department of CSE
  • 13. Contiguous MemoryAllocation (calloc) • Calloc is primarily used to allocate memory for arrays. • It differs from malloc only in that it sets memory to null characters. • void *calloc (size_t element-count,size_t element-size) 13 Department of CSE
  • 14. Reallocation of memory(realloc) • The realloc function can be highly inefficient and should be used advisedly. • When given a pointer to the previously allocated block of memory, realloc changes the size of the block by deleting or extending the memory at the end of the block. • If memory cannot be extended because of other allocations, realloc allocates a completely new block and copies the existing memory allocation to new allocation, and deletes the old allocation. • void *realloc (void* ptr,size_t newSize) 14 Department of CSE
  • 16. Releasing Memory (free) • When memory locations allocated by malloc, calloc or realloc are no longer needed, they should be freed using the predefined function free. • void free(void* ptr) • Below shows the example where first one releases a single element allocated with malloc • Second example shows 200 elements were allocated with calloc . When free the pointer 200 elements are returned to the heap. 16 Department of CSE
  • 18. Difference between malloc and calloc S.no malloc() calloc() 1 It allocates only single block of requested memory It allocates multiple blocks of requested memory 2 int *ptr;ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory only allocated in one block. int *ptr;Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory will be created and each contains 20*4 bytes of memory. Total = 80 bytes Total = 1600 bytes 3 malloc () doesn’t initializes the allocated memory. It contains garbage values calloc () initializes the allocated memory to zero 4 type cast must be done since this function returns void pointer int *ptr;ptr = (int*)malloc(sizeof(int)*20 ); Same as malloc () function int *ptr;ptr = (int*)calloc( 20, 20 * sizeof(int) ); 18 Department of CSE
  • 19. Resizing and Releasing Memory • When your program comes out, operating system automatically release all the memory allocated by your program but as a good practice when you are not in need of memory anymore then you should release that memory by calling the function free(). • Alternatively, you can increase or decrease the size of an allocated memory block by calling the function realloc(). 19 Department of CSE
  • 20. Example-1 #include <stdio.h> #include <stdlib.h> int main(){ int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: "); for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr); return 0; } 20 Department of CSE Enter number of elements: 3 Enter elements of array: 2 7 1 Sum=10 Dynamic-ex1.c
  • 21. Example - 2 21 Department of CSE #include <stdio.h> #include <stdlib.h> int main(){ int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)calloc(n,sizeof(int)); if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: "); for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr); return 0; } Enter number of elements: 3 Enter elements of array: 2 1 3 Sum=6 Dynamic-ex2.c
  • 22. Example - 3 22 Department of CSE #include <stdio.h> #include <stdlib.h> int main(){ int *ptr,i,n1,n2; printf("Enter size of array: "); scanf("%d",&n1); ptr=(int*)malloc(n1*sizeof(int)); printf("Address of previously allocated memory: "); for(i=0;i<n1;++i) printf("%ut",ptr+i); printf("nEnter new size of array: "); scanf("%d",&n2); ptr=realloc(ptr,n2); for(i=0;i<n2;++i) printf("%ut",ptr+i); return 0; } Enter size of array: 3 Address of previously allocated memory: 7474944 7474948 7474952 Enter new size of array: 5 7474944 7474948 7474952 7474956 7474960 Dynamic-ex3.c
  • 23. Example - 4 #include <stdio.h> #include <string.h> int main() { char name[100]; char *description; strcpy(name,"Zara Ali"); /* allocate memory dynamically */ description = malloc( 200 * sizeof(char) ); if( description == NULL ) { fprintf(stderr,"Error - unable to allocate required memoryn"); } else { strcpy( description,"Zara ali a DPS student in class 10th"); } printf("Name = %sn", name ); printf("Description: %sn",description ); } 23 Department of CSE Name = Zara Ali Description:Zara ali a DPS student in class 10th Dynamic-ex4.c
  • 24. Example - 5 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char name[100]; char *description; strcpy(name,"Zara Ali"); /* allocate memory dynamically */ description = malloc( 30 * sizeof(char) ); if( description == NULL ) { fprintf(stderr,"Error - unable to allocate required memoryn"); } else { strcpy( description,"Zara ali a DPS student."); } 24 Department of CSE
  • 25. Example – 5 Cont--- /* suppose you want to store bigger description */ description = realloc( description,100 * sizeof(char) ); if( description == NULL ) { fprintf(stderr, "Error - unable to allocate required memoryn"); } else { strcat( description, "She is in class 10th"); } printf("Name = %sn", name ); printf("Description: %sn", description ); /* release memory using free() function */ free(description); } 25 Department of CSE Name = Zara Ali Description: Zara ali a DPS student.She is in class 10th Dynamic-ex5.c
  • 26. Memory Leaks • A memory leak occurs when allocated memory is never used again but is not freed. • I can happen when • The memory’s address is lost • The free function is never invoked though it should be • The problem with memory leak is that the memory cannot be reclaimed and use later. The amount of memory available t the heap manager will be decreased. • If the memory is repeatedly allocated and then lost, then the program may terminate when more memory is needed but malloc cannot allocate it because it ran out of memory. 26 Department of CSE
  • 27. Example char *chunk; while(10 { chunk=(char*) malloc (1000000); printf(“allocatingn”); } • The variable chunk is assigned memory from heap. However this memory is not freed before another block of memory is assigned to it. • Eventfully the application will run out of memory and terminate abnormally. 27 Department of CSE
  • 29. PointerArithmetic • This section introduces the concept of pointer arithmetic, and this will form one of the very important building blocks in understanding the functionality of pointers. 29 Department of CSE
  • 30. Pointer Expressions and PointerArithmetic • Arithmetic operations can be performed on pointers • Increment/decrement pointer (++ or --) • Add an integer to a pointer( + or += , - or -=) • Pointers may be subtracted from each other • All these operations meaningless unless performed on an array • NOTE: Division and Multiplication are not allowed.
  • 31. PointerArithmetic • int a,b,*p,*q • p=-q /* illegal use of pointers*/ • p<<=1 /* illegal use of pointers*/ • p=p-b /*valid*/ • p=p-q /* nonportable pointer conversion*/ • p=(int*) p-q /*valid*/ • p=p-q-a /*valid*/ • p=p+a /*valid*/ • p=p+q /* invalid pointer addition*/ • p=p*q /* illegal use of pointers*/ • p=p/q /* illegal use of pointers*/ • p=p/a /* illegal use of pointers*/ 31 Department of CSE
  • 32. Pointer Increment – Example - 1 #include<stdio.h> void main() { int n; int *pn; pn=&n; int *pn1; pn1=pn+1; printf("%d %dn", pn,pn1); double d; double *pd; pd=&d; double *pd1; pd1=pd+1; printf("%d %dn", pd,pd1); } 32 Department of CSE 2686788 2686792 2686768 2686776 Arithmetic-ex1.c
  • 33. Incrementing Pointer : • Incrementing Pointer is generally used in array because we have contiguous memory in array and we know the contents of next memory location. • Incrementing PointerVariable Depends Upon data type of the Pointer variable 33 Department of CSE
  • 34. Example – 2 #include<stdio.h> int main() { int *ptr=(int *)1000; printf(“OldValue of ptr : %u",ptr); ptr=ptr+1; printf("NewValue of ptr : %u",ptr); return 0; } 34 Department of CSE OldValue of ptr : 1000 NewValue of ptr : 1004 Arithmetic-ex2.c
  • 35. Difference between two integer Pointers – Example - 3 #include<stdio.h> int main(){ float *ptr1=(float *)1000; float *ptr2=(float *)2000; printf("nDifference : %dn",ptr2-ptr1); return 0; } 35 Department of CSE Difference : 250 Arithmetic-ex3.c
  • 36. Explanation • Ptr1 and Ptr2 are two pointers which holds memory address of Float Variable. • Ptr2-Ptr1 will gives us number of floating point numbers that can be stored. • ptr2 - ptr1 = (2000 - 1000) / sizeof(float) • = 1000 / 4 36 Department of CSE
  • 37. Pointer Division – Example - 4 #include<stdio.h> int main() { int *ptr1,*ptr2; ptr1 = (int *)1000; ptr2 = ptr1/4; return(0); } 37 Department of CSE Illegal Use of operator : INVALID Arithmetic-ex4.c
  • 38. Pointer Expressions and PointerArithmetic-Arrays • 5 element int array on machine with 4 byte ints • vPtr points to first element v[ 0 ] • at location 3000 (vPtr = 3000) • vPtr += 2; sets vPtr to 3008 • vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to address 3008 Array v and a pointer variable vPtr that points to v.
  • 39. The pointer vPtr after pointer arithmetic
  • 40. Pointer Expressions and PointerArithmetic • Subtracting pointers • Returns number of elements from one to the other. If vPtr2 = v[ 2 ]; vPtr = v[ 0 ]; • vPtr2 - vPtr would produce 2 • Pointer comparison ( <, == , > ) • See which pointer points to the higher numbered array element • Also, see if a pointer points to 0
  • 41. The Relationship Between Pointers and Arrays • Arrays and pointers closely related • Array name like a constant pointer • Pointers can do array subscripting operations • Define an array b[ 5 ] and a pointer bPtr • To set them equal to one another use: bPtr = b; • The array name (b) is actually the address of first element of the array b[ 5 ] bPtr = &b[ 0 ] • Explicitly assigns bPtr to address of first element of b
  • 42. The Relationship Between Pointers and Arrays • Element b[ 3 ] • Can be accessed by *( bPtr + 3 ) • Where n is the offset. Called pointer/offset notation • Can be accessed by bptr[ 3 ] • Called pointer/subscript notation • bPtr[ 3 ] same as b[ 3 ] • Can be accessed by performing pointer arithmetic on the array itself *( b + 3 )
  • 43. Example - 5 1 2 Using subscripting and pointer notations with arrays */ 3 4 #include <stdio.h> 5 6 int main( void ) 7 { 8 int b[] = { 10, 20, 30, 40 }; /* initialize array b */ 9 int *bPtr = b; /* set bPtr to point to array b */ 10 int i; /* counter */ 11 int offset; /* counter */ 12 13 /* output array b using array subscript notation */ 14 printf( "Array b printed with:nArray subscript notationn" ); 15 16 /* loop through array b */ 17 for ( i = 0; i < 4; i++ ) { 18 printf( "b[ %d ] = %dn", i, b[ i ] ); 19 } /* end for */ 20 21 /* output array b using array name and pointer/offset notation */ 22 printf( "nPointer/offset notation wheren" 23 "the pointer is the array namen" ); 24 25 /* loop through array b */ 26 for ( offset = 0; offset < 4; offset++ ) { 27 printf( "*( b + %d ) = %dn", offset, *( b + offset ) ); 28 } /* end for */ 29 Array subscript notation Pointer/offset notation
  • 44. 30 /* output array b using bPtr and array subscript notation */ 31 printf( "nPointer subscript notationn" ); 32 33 /* loop through array b */ 34 for ( i = 0; i < 4; i++ ) { 35 printf( "bPtr[ %d ] = %dn", i, bPtr[ i ] ); 36 } /* end for */ 37 38 /* output array b using bPtr and pointer/offset notation */ 39 printf( "nPointer/offset notationn" ); 40 41 /* loop through array b */ 42 for ( offset = 0; offset < 4; offset++ ) { 43 printf( "*( bPtr + %d ) = %dn", offset, *( bPtr + offset ) ); 44 } /* end for */ 45 46 return 0; /* indicates successful termination */ 47 48 } /* end main */ Array b printed with: Array subscript notation b[ 0 ] = 10 b[ 1 ] = 20 b[ 2 ] = 30 b[ 3 ] = 40 (continued on next slide… ) Pointer subscript notation Pointer offset notation Arithmetic-ex5.c
  • 45. (continued from previous slide…) Pointer/offset notation where the pointer is the array name *( b + 0 ) = 10 *( b + 1 ) = 20 *( b + 2 ) = 30 *( b + 3 ) = 40 Pointer subscript notation bPtr[ 0 ] = 10 bPtr[ 1 ] = 20 bPtr[ 2 ] = 30 bPtr[ 3 ] = 40 Pointer/offset notation *( bPtr + 0 ) = 10 *( bPtr + 1 ) = 20 *( bPtr + 2 ) = 30 *( bPtr + 3 ) = 40
  • 46. 1 2 Copying a string using array notation and pointer notation. */ 3 #include <stdio.h> 4 5 void copy1( char * const s1, const char * const s2 ); /* prototype */ 6 void copy2( char *s1, const char *s2 ); /* prototype */ 7 8 int main( void ) 9 { 10 char string1[ 10 ]; /* create array string1 */ 11 char *string2 = "Hello"; /* create a pointer to a string */ 12 char string3[ 10 ]; /* create array string3 */ 13 char string4[] = "Good Bye"; /* create a pointer to a string */ 14 15 copy1( string1, string2 ); 16 printf( "string1 = %sn", string1 ); 17 18 copy2( string3, string4 ); 19 printf( "string3 = %sn", string3 ); 20 21 return 0; /* indicates successful termination */ 22 23 } /* end main */ 24
  • 47. 25 /* copy s2 to s1 using array notation */ 26 void copy1( char * const s1, const char * const s2 ) 27 { 28 int i; /* counter */ 29 30 /* loop through strings */ 31 for ( i = 0; ( s1[ i ] = s2[ i ] ) != '0'; i++ ) { 32 ; /* do nothing in body */ 33 } /* end for */ 34 35 } /* end function copy1 */ 36 37 /* copy s2 to s1 using pointer notation */ 38 void copy2( char *s1, const char *s2 ) 39 { 40 /* loop through strings */ 41 for ( ; ( *s1 = *s2 ) != '0'; s1++, s2++ ) { 42 ; /* do nothing in body */ 43 } /* end for */ 44 45 } /* end function copy2 */ string1 = Hello string3 = Good Bye Condition of for loop actually performs an action
  • 49. Introduction • An array of pointer is similar to an array of ant predefined data type • As a pointer variable always contain an address, an array of pointer is a collection of addresses. • These can be address of ordinary isolated variable or of array elements. • The elements of an array of pointers are stored in the memory just like elements of any other kind of array. • Example is given below..
  • 50. Example - 1 #include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr[MAX]; //array of pointers for ( i = 0; i < MAX; i++) { ptr[i] = &var[i]; /* assign the address of integer. */ } for ( i = 0; i < MAX; i++) { printf("Value of var[%d] = %dn", i, *ptr[i] ); } return 0; } Value of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200 Arraypointer-ex1.c
  • 51. Example - 2 #include<stdio.h> void main() { int arr[3]={1,2,3}; int i, *ptr[3]; for(i=0;i<3;i++) ptr[i]=arr+i; for(i=0;i<3;i++) printf("%p %dn", ptr[i],*ptr[i]); } 51 Department of CSE 0028FF30 1 0028FF34 2 0028FF38 3 Arraypointer-ex2.c
  • 52. Arrays of Pointers - Strings • Arrays can contain pointers • For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; • Strings are pointers to the first character • char * – each element of suit is a pointer to a char • The strings are not actually stored in the array suit, only pointers to the strings are stored • suit array has a fixed size, but strings can be of any size
  • 53. Case study: Roman numeral equivalents – Example - 3 #include <stdio.h> void main( void ) { int decimal_number = 101, a = 0, b = 0; const char *x[11] = {"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc", "c"}; const char *y[10] = {"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"}; while ((decimal_number > 100) || (decimal_number < 0)) { printf("Enter the decimal numbers in the range 1 to 100:n"); scanf("%d", &decimal_number); } a = decimal_number/10; b = decimal_number%10; printf("The equivalent roman is %s%sn", x[a], y[b]); } Enter the decimal numbers in the range 1 to 100: 15 The equivalent roman is xv Arraypointer-ex3.c
  • 54. Review • Dynamic Memory allocation • PointerArithmetic • Array of pointers
  • 55. Try itYourself • Write a program using dynamic memory allocation to get a student’s mark and display it back. • Write a program to do the following The process for finding a prime is quite simple. First, you know by inspection that 2, 3, and 5 are the first three prime numbers, because they aren’t divisible by anything other than 1 and themselves. Because all the other prime numbers must be odd (otherwise they would be divisible by 2), you can work out the next number to check by starting at the last prime you have and adding 2. When you’ve checked out that number, you add another 2 to get the next to be checked, and so on. to check whether a number is actually prime rather than just odd, you could divide by all the odd numbers less than the number that you’re checking, but you don’t need to do as much work as that. if a number is not prime, it must be divisible by one of the primes lower than the number you’re checking. Because you’ll obtain the primes in sequence, it will be sufficient to check a candidate by testing whether any of the primes you’ve already found is an exact divisor. Store all primes in an array using pointers and pointer arithmetic. 55 Department of CSE
  • 56. Try itYourself • Find Largest Element Using Dynamic Memory Allocation and pointer arithmetic to access the array • Write a C program to find the sum of two 1D matrices using dynamic memory and pointer arithmetic. 56 Department of CSE
  • 57. Try itYourself –Ans - Student mark #include<stdio.h> #include<stdlib.h> void main() { int no, *pt,i; clrscr(); printf("Enter no of Students :"); scanf("%d",&no); pt=(int *)malloc(no*2); if(pt== NULL) { printf("nnMemory allocation failed!"); exit(0); } 57 Department of CSE
  • 58. Student mark printf("* * * * Enter roll no of students. * * * *n"); for (i=0;i<no;i++) { printf("-->"); scanf("%d",(pt+i)); } printf("n* * * * Entered roll no. * * * *n"); for (i=0;i<no;i++) { printf("%d, ",*(pt+i)); } } 58 Department of CSE