Recall
• What are functions used for?
• What happens when we declare a
varaible? Say int a;
• What is the difference betwe...
Introduction to C
Memory Management
Week 3- day 1
When does memory
allocated in C?
• There are two ways in which memory can
be allocated in C:
–by declaring variables
–by e...
Where does memory
allocated in C?
–by declaring variables -
Stack
–by explicitly requesting space from C
- Heap
Stack
Stack
• Stack is the place where all the
variables that are declared and
initialized before runtime are stored.
• Stack fo...
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()
...
What if there is no enough stack
space?
Stack
Main()
Aaa()
Bbb()
Ccc()
Ddd()
Eee()
Fff()Stack Over flow
Heap
Heap
• Heap is the area of memory used for
dynamic memory allocation
• Programmer allocates memory manually
at heap ; and ...
How to allocate space in heap?
• C inbuilt functions
–Malloc() Allocates the
specified number of bytes
–Calloc() Allocates...
Malloc()
• malloc() stands for "memory allocation"
• The malloc() function dynamically
allocates memory when required.
• T...
Malloc()
• Int *p;
• P=malloc(2);
HEAP
2 byte memory
Stack
1000
1000
All are same !
int *p;
p=malloc(2); //allocates 2 bytes of
memory
p=malloc(sizeof(Int)); //sizeof() returns
size of any da...
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",...
calloc()
• Calloc() stands for "contiguous allocation”
• Allocates multiple blocks of memory each of
same size and sets al...
calloc()
• Int *p;
• P=calloc(3,10);
HEAP
10 bytes size
Stack
1000
1000
10 bytes size
10 bytes size
1001
1010
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",...
Malloc() Vs Calloc()
• Allocates "size" bytes of memory.
• The contents of
allocated memory are not changed.
i.e., the mem...
Structure of Ram
Structure of Ram
TEXT
Also known as the code segment,
holds the executable instructions
of a program.
• execute-only
• fix...
Structure of Ram
A global variable that is initialized
and stored in the data segment.
This section has read/write
attribu...
Structure of Ram
This section holds uninitialized
data. This data consists of global
variables that the system
initializes...
Structure of Ram
When a program uses malloc() to
obtain dynamic memory, this
memory is placed in the heap.
Structure of Ram
This contains all the local
variables that get allocated.
When a function is called, the
local variables ...
Questions?
“A good question deserve a good
grade…”
Self Check !!
Self Check
• When the program execution ends
variables in heap will be automatically
released
–True
–False
Self Check
• When the program execution ends
variables in heap will be automatically
released
–True
–False
Self Check
• Reference to the heap memory will be
stored in
•Heap
•Stack
•Gvar
•BSS
Self Check
• Reference to the heap memory will be
stored in
•Heap
•Stack
•Gvar
•BSS
Self Check
• Difference between malloc() and
calloc()
•Number of arguments
•No of blocks
•Initialization
•All of above
•No...
Self Check
• Difference between malloc() and
calloc()
•Number of arguments
•No of blocks
•Initialization
•All of above
•No...
Self Check
• Complete the below
Main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(in...
Self Check
• Complete the below
Main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(in...
Self Check
Free()
Malloc()
Heap
Callloc()
Stack
Variables
2 arguments
Automatically free the
memory
Automatically allocate...
Self Check
Free()
Malloc()
Heap
Callloc()
Stack
Variables
2 arguments
Automatically free the
memory
Automatically allocate...
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(“En...
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(“En...
End of Day 1
Upcoming SlideShare
Loading in …5
×

Introduction to c part -3

520 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
520
On SlideShare
0
From Embeds
0
Number of Embeds
97
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to c part -3

  1. 1. 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 ?
  2. 2. Introduction to C Memory Management Week 3- day 1
  3. 3. 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
  4. 4. Where does memory allocated in C? –by declaring variables - Stack –by explicitly requesting space from C - Heap
  5. 5. Stack
  6. 6. 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
  7. 7. 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
  8. 8. What if there is no enough stack space? Stack Main() Aaa() Bbb() Ccc() Ddd() Eee() Fff()Stack Over flow
  9. 9. Heap
  10. 10. 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 stack23 32 43 452 89
  11. 11. 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
  12. 12. 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 Syntax malloc(size_in_bytes);
  13. 13. Malloc() • Int *p; • P=malloc(2); HEAP 2 byte memory Stack 1000 1000
  14. 14. 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
  15. 15. #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=10 1000ptr=1000
  16. 16. 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_byt es); Again we need a pointer to point to the allocated space in heap
  17. 17. calloc() • Int *p; • P=calloc(3,10); HEAP 10 bytes size Stack 1000 1000 10 bytes size 10 bytes size 1001 1010
  18. 18. #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 }
  19. 19. 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.
  20. 20. Structure of Ram
  21. 21. Structure of Ram TEXT Also known as the code segment, holds the executable instructions of a program. • execute-only • fixed size
  22. 22. 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.
  23. 23. 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.
  24. 24. Structure of Ram When a program uses malloc() to obtain dynamic memory, this memory is placed in the heap.
  25. 25. 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.
  26. 26. Questions? “A good question deserve a good grade…”
  27. 27. Self Check !!
  28. 28. Self Check • When the program execution ends variables in heap will be automatically released –True –False
  29. 29. Self Check • When the program execution ends variables in heap will be automatically released –True –False
  30. 30. Self Check • Reference to the heap memory will be stored in •Heap •Stack •Gvar •BSS
  31. 31. Self Check • Reference to the heap memory will be stored in •Heap •Stack •Gvar •BSS
  32. 32. Self Check • Difference between malloc() and calloc() •Number of arguments •No of blocks •Initialization •All of above •None of above
  33. 33. Self Check • Difference between malloc() and calloc() •Number of arguments •No of blocks •Initialization •All of above •None of above
  34. 34. Self Check • Complete the below Main() { int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*) malloc (n*sizeof(int)); }
  35. 35. Self Check • Complete the below Main() { int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*) malloc (n*sizeof(int)); }
  36. 36. 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
  37. 37. 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
  38. 38. 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]); Self Check
  39. 39. 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]); Self Check
  40. 40. End of Day 1

×