Introduction to c part -3
-
Upload
baabtracom-first-coding-school-in-india -
Category
Technology
-
view
357 -
download
1
Transcript of Introduction to c part -3
![Page 1: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/1.jpg)
Recall
• What are functions used for?• What happens when we declare a
varaible? Say int a;• What is the difference between array
and structure?• What is the output if i print *p, p, &p
?
![Page 2: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/2.jpg)
Introduction to CMemory Management
Week 3- day 1
![Page 3: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/3.jpg)
When does memory allocated in C?
• There are two ways in which memory can be allocated in C:
–by declaring variables–by explicitly requesting space from
C
• Each case above space is allocated at different sectors of RAM
![Page 4: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/4.jpg)
Where does memory allocated in C?
–by declaring variables - Stack–by explicitly requesting space from
C - Heap
![Page 5: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/5.jpg)
Stack
![Page 6: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/6.jpg)
Stack
• Stack is the place where all the variables that are declared and initialized before runtime are stored.
• Stack follows Last in First Out order(LIFO)
• When the program finishes stack will release all the spaces allocated for variables.
Stack
12
32
322
34
![Page 7: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/7.jpg)
main(){
Int a=10,b=20,c;c=sum(a,b);
}int sum(int a,int b){
int c;c=a+b;return c;
}
RAM
Main()
a=10
b=20
c =30
sum()
a =10
b=20
C=30
![Page 8: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/8.jpg)
What if there is no enough stack space?
Stack
Main()
Aaa()
Bbb()
Ccc()
Ddd()
Eee()
Fff()Stack Over flow
![Page 9: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/9.jpg)
Heap
![Page 10: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/10.jpg)
Heap
• Heap is the area of memory used for dynamic memory allocation
• Programmer allocates memory manually at heap ; and hence variable on the heap must be destroyed manually. There is no automatic release as in stack
23 32
43
45289
![Page 11: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/11.jpg)
How to allocate space in heap?
• C inbuilt functions–Malloc() Allocates the
specified number of bytes
–Calloc() Allocates the specified number of bytes
and initializes them to zero
–Realloc() Increases or decreases the size of the
specified block of memory. Reallocates it if needed
–Free() Releases the specified block of memory
back to the system
![Page 12: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/12.jpg)
Malloc()
• malloc() stands for "memory allocation"
• The malloc() function dynamically allocates memory when required.
• This function allocates ‘size’ byte of memory and returns a pointer to the first byte or NULL if there is some kind of error
Syntaxmalloc(size_in_bytes);
But we need a pointer to point to the allocated space in heap
![Page 13: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/13.jpg)
Malloc()
• Int *p;• P=malloc(2);
HEAP
2 byte memory
Stack
1000
1000
![Page 14: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/14.jpg)
All are same !
int *p;p=malloc(2); //allocates 2 bytes
of memory
p=malloc(sizeof(Int)); //sizeof() returns size of any data type
so here it will be mallloc(2)
p=(int *)malloc(2); //malloc always return void * pointer . So you may type cast it into any data type.
but it is unnecessary as the compiler does it for us
p=(int *) (sizeof(int)); //allocates 2 bytes of memory type
casted to the integer type pointer
![Page 15: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/15.jpg)
#include <stdio.h>#include <stdlib.h>void 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 the elements of Array”);For(i=0;i<n;++i){
Scanf(“%d”,ptr+i);Sum+=*(ptr+i);
}Printf(“sum=%d”,sum);free(ptr); // allocated memory is released
}
HEAPStack
n=101000ptr=1000
![Page 16: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/16.jpg)
calloc()
• Calloc() stands for "contiguous allocation”• Allocates multiple blocks of memory each
of same size and sets all bytes to zero
• Syntax
calloc(number_of_elements,size_in_bytes);
Again we need a pointer to point to the allocated space in heap
![Page 17: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/17.jpg)
calloc()
• Int *p;• P=calloc(3,10);
HEAP
10 bytes size
Stack
10001000
10 bytes size
10 bytes size
1001
1010
![Page 18: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/18.jpg)
#include <stdio.h>#include <stdlib.h>void main(){
int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc if(ptr==NULL)
{ printf("Error! memory not allocated."); exit(0); }Printf(“Enter the elements of Array”);For(i=0;i<n;++i){
Scanf(“%d”,ptr+i); // Scanf(“%d”,&ptr[i]);Sum+=*(ptr+i);
}Printf(“sum=%d”,sum);free(ptr); // allocated memory is released
}
![Page 19: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/19.jpg)
Malloc() Vs Calloc()
• Allocates "size" bytes of memory.
• The contents of allocated memory are not changed. i.e., the memory may contains garbage values.
• returns void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned.
• Allocates a region of memory large enough to hold "n elements" of "size" bytes each.
• The allocated region is initialized to zero.
• returns void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned.
![Page 20: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/20.jpg)
Structure of Ram
![Page 21: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/21.jpg)
Structure of Ram
TEXTAlso known as the code segment, holds the executable instructions of a program.• execute-only• fixed size
![Page 22: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/22.jpg)
Structure of Ram
A global variable that is initialized and stored in the data segment.
This section has read/write attributes but cannot be shared among processes running the
same program.
![Page 23: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/23.jpg)
Structure of Ram
This section holds uninitialized data. This data consists of global
variables that the system initializes with 0s upon program
execution. Another name for this section is the zero-initialized data
section.
![Page 24: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/24.jpg)
Structure of Ram
When a program uses malloc() to obtain dynamic memory, this
memory is placed in the heap.
![Page 25: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/25.jpg)
Structure of Ram
This contains all the local variables that get allocated. When
a function is called, the local variables for that function are
pushed onto the stack.
![Page 26: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/26.jpg)
Questions?“A good question deserve a
good grade…”
![Page 27: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/27.jpg)
Self Check !!
![Page 28: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/28.jpg)
Self Check
• When the program execution ends variables in heap will be automatically released
–True–False
![Page 29: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/29.jpg)
Self Check
• When the program execution ends variables in heap will be automatically released
–True–False
![Page 30: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/30.jpg)
Self Check
• Reference to the heap memory will be stored in
•Heap•Stack•Gvar•BSS
![Page 31: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/31.jpg)
Self Check
• Reference to the heap memory will be stored in
•Heap•Stack•Gvar•BSS
![Page 32: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/32.jpg)
Self Check
• Difference between malloc() and calloc()
•Number of arguments•No of blocks• Initialization•All of above•None of above
![Page 33: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/33.jpg)
Self Check
• Difference between malloc() and calloc()
•Number of arguments•No of blocks• Initialization•All of above•None of above
![Page 34: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/34.jpg)
Self Check
• Complete the belowMain(){
int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*) malloc (n*sizeof(int));
}
![Page 35: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/35.jpg)
Self Check
• Complete the belowMain(){
int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*) malloc (n*sizeof(int));
}
![Page 36: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/36.jpg)
Self Check
Free()
Malloc()
Heap
Callloc()
Stack
Variables
2 arguments
Automatically free the memory
Automatically allocated in stack
Reserves one block of memory
Should be used when memory is dynamically allocated
Reference in stack
![Page 37: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/37.jpg)
Self Check
Free()
Malloc()
Heap
Callloc()
Stack
Variables
2 arguments
Automatically free the memory
Automatically allocated in stack
Reserves one block of memory
Should be used when memory is dynamically allocated
Reference in stack
![Page 38: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/38.jpg)
main(){
int i,a[10];for(i=0;i<10;i++){
printf(“Enter the number”);
scanf(“%d”,&a[i]);}
}
main(){
int i,*p,n;printf(“Enter the number of elements”);scanf(“%d”,&n);*p=calloc(n,sizeof(int));for(i=0;i<10;i++){
printf(“Enter the number”);
scanf(“%d”,&p [i]);}free(p);
}
Self Check
![Page 39: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/39.jpg)
main(){
int i,a[10];for(i=0;i<10;i++){
printf(“Enter the number”);
scanf(“%d”,&a[i]);}
}
main(){
int i,*p,n;printf(“Enter the number of elements”);scanf(“%d”,&n);*p=calloc(n,sizeof(int));for(i=0;i<10;i++){
printf(“Enter the number”);
scanf(“%d”,&p [i]);}free(p);
}
Self Check
![Page 40: Introduction to c part -3](https://reader038.fdocuments.in/reader038/viewer/2022110119/5562095bd8b42a00138b474e/html5/thumbnails/40.jpg)
End of Day 1