Dynamic Allocation

P. P. Chakrabarti

1

13-03-03

P.P.Chakrabarti, IIT Kharagpur

Fun with scope rules:
int data=0, test=1; [ppchak]$ ./a.out main() M1: i = 2, j = 3 { int i=2, j=3, test=2; 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 M4: test = 2, data = 0 int data = 1, i =5; 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; [ppchak]$ ./a.out main() M1: i = 2, j = 3 { int i=2, j=3, test=2; 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 M4: test = 2, data = 0 int data = 1; i =5; 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; printf("M1: i = %d, j = %d\n", i,j); printf("M2: test = %d, data = %d\n", test, data); fun(); } 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); }

[ppchak]$ ./a.out M1: i = 2, j = 3 M2: test = 2, data = 0 F1: i = 6, j = 7 F2: test = 1, data = 5

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 Free memory Global variables Instructions

Stack Heap Permanent 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( )
main() [ppchak]$ ./a.out { 5 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.
“malloc(n*sizeof(int))” Allocates a chunk of memory of total size = n * sizeof(int) Returns pointer to first byte

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

#define N 20 #define M 10 main() { char word[N], *w[M]; int i, n; scanf("%d",&n); for (i=0; i<n; ++i) { 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]); }

[ppchak]$ ./a.out 4 Tendulkar Sourav Khan India w[0] = Tendulkar w[1] = Sourav w[2] = Khan w[3] = India

10

13-03-03

P.P.Chakrabarti, IIT Kharagpur

How it will look like
w 0 1 2 3
T e n u a d d r u a l k a r ###BOT_TEXT### S o K h I n v ###BOT_TEXT###

n ###BOT_TEXT### i a ###BOT_TEXT###

9

11

13-03-03

P.P.Chakrabarti, IIT Kharagpur

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

12

13-03-03

P.P.Chakrabarti, IIT Kharagpur

How this will look like
w 0 1 2 3 4
I n d s n i t y a ###BOT_TEXT### r a l i a ###BOT_TEXT### A u K e

a ###BOT_TEXT### e a a n l k a n d ###BOT_TEXT###

N e w Z S r i L

a ###BOT_TEXT###

13

13-03-03

P.P.Chakrabarti, IIT Kharagpur