You are on page 1of 13

Dynamic Allocation

P. P. Chakrabarti

1 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Fun with scope rules:
int data=0, test=1;
main() [ppchak]$ ./a.out
{ int i=2, j=3, test=2; M1: i = 2, j = 3
M2: test = 2, data = 0
printf("M1: i = %d, j = %d\n", i,j);
B1: i = 5, j = 3
printf("M2: test = %d, data = %d\n", test, data); B2: test = 2, data = 1
{ M3: i = 2, j = 3
int data = 1, i =5; M4: test = 2, data = 0
printf("B1: i = %d, j = %d\n", i,j);
printf("B2: test = %d, data = %d\n", test, data);
}
printf("M3: i = %d, j = %d\n", i,j);
printf("M4: test = %d, data = %d\n", test, data);
}

2 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Spot the difference:
int data=0, test=1;
main() [ppchak]$ ./a.out
{ int i=2, j=3, test=2; M1: i = 2, j = 3
M2: test = 2, data = 0
printf("M1: i = %d, j = %d\n", i,j);
B1: i = 5, j = 3
printf("M2: test = %d, data = %d\n", test, data); B2: test = 2, data = 1
{ M3: i = 5, j = 3
int data = 1; i =5; M4: test = 2, data = 0
printf("B1: i = %d, j = %d\n", i,j);
printf("B2: test = %d, data = %d\n", test, data);
}
printf("M3: i = %d, j = %d\n", i,j);
printf("M4: test = %d, data = %d\n", test, data);
}

3 13-03-03 P.P.Chakrabarti, IIT Kharagpur


More on scope:
int data=0, test=1;
main()
{ int i=2, j=3, test=2; [ppchak]$ ./a.out
M1: i = 2, j = 3
printf("M1: i = %d, j = %d\n", i,j);
M2: test = 2, data = 0
printf("M2: test = %d, data = %d\n", test, data); F1: i = 6, j = 7
fun(); F2: test = 1, data = 5
}
fun()
{ int data =5;
int i=6, j =7;
printf("F1: i = %d, j = %d\n", i,j);
printf("F2: test = %d, data = %d\n", test, data);
}

4 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Problems with static allocation
Scenario 1: Read an integer n, read n integers and sort
them.
[Q: How do we declare the array size?]
Scenario 2: Make the set ADT with the insert, delete,
member, union functions with no idea of how many
elements may exist at a given point in time.
[Q: Can we use an array at all for this?]
[ How to prevent space wastage in these scenarios?]
To solve these, C provides scope for dynamically
allocating and releasing memory using library
functions

5 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Memory Allocation Process in C
Used for dynamic
allocation

Local variables Stack

Free memory Heap


Global variables Permanent
Instructions storage area

6 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Memory Allocation Functions
? malloc: Allocates requested number of bytes and
returns a pointer to the first byte of the allocated
space.
? calloc: Allocates space for an array of elements,
initializes them to zero and then returns a pointer to
the memory.
? free : Frees previously allocated space.
? realloc: Modifies the size of previously allocated
space.

7 13-03-03 P.P.Chakrabarti, IIT Kharagpur


malloc( )
“malloc(n*sizeof(int))”
main() Allocates a chunk of memory [ppchak]$ ./a.out
{ of total size = n * sizeof(int) 5
Returns pointer to first byte
int *A, n, i; 5
scanf("%d", &n); 4
A = (int *) malloc(n*sizeof(int)); 7
for (i=0; i<n; i++) scanf("%d", &A[i]); 9
for (i=0; i<n; i++) printf("A[%d] = %d\n",i,A[i]); 2
} A[0] = 5
A[1] = 4
A[2] = 7
(int *) “type casts” the returned pointer A[3] = 9
to point to integers making it compatible A[4] = 2
to the type for A.

8 13-03-03 P.P.Chakrabarti, IIT Kharagpur


malloc( )-ing records
main()
{ typedef struct{
char name[20];
int roll;
float SGPA[8], CGPA;
} person;
person *student;
int i,j,n;
scanf("%d", &n);
student = (person *)malloc(n*sizeof(person));
for (i=0; i<n; i++) {
scanf("%s", student[i].name);
scanf("%d", &student[i].roll);
for(j=0;j<8;j++) scanf("%f", &student[i].SGPA[j]);
}
}

9 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Arrays of pointers [ppchak]$ ./a.out
#define N 20 4
Tendulkar
#define M 10
Sourav
main() Khan
{ char word[N], *w[M]; India
int i, n; w[0] = Tendulkar
scanf("%d",&n); w[1] = Sourav
w[2] = Khan
for (i=0; i<n; ++i) {
w[3] = India
scanf("%s", word);
w[i] = (char *) calloc (strlen(word)+1, sizeof(char));
strcpy (w[i], word) ;
}
for (i=0; i<n; i++) printf("w[%d] = %s \n",i,w[i]);
}

10 13-03-03 P.P.Chakrabarti, IIT Kharagpur


How it will look like
w
0 T e n d u l k a r \0
1 S o u r a v \0

2 K h a n \0

3 I n d i a \0

11 13-03-03 P.P.Chakrabarti, IIT Kharagpur


Dynamic Arrays of pointers
#define N 20
main()
{ [ppchak]$ ./a.out
char word[N], **w; /* “**w” is a pointer to a pointer array */ 5
int i, n; India
scanf("%d",&n); Australia
w = (char **) malloc (n * sizeof(char *)); Kenya
for (i=0; i<n; ++i) { NewZealand
scanf("%s", word); SriLanka
w[i] = (char *) calloc (strlen(word)+1, sizeof(char)); w[0] = India
strcpy (w[i], word) ; w[1] = Australia
} w[2] = Kenya
for (i=0; i<n; i++) printf("w[%d] = %s \n",i,w[i]); w[3] = NewZealand
} w[4] = SriLanka

12 13-03-03 P.P.Chakrabarti, IIT Kharagpur


How this will look like
w
0 I n d i a \0
1 A u s t r a l i a \0
2 K e n y a \0
3 N e w Z e a l a n d \0
4 S r i L a n k a \0

13 13-03-03 P.P.Chakrabarti, IIT Kharagpur

You might also like