Dynamic Memory Allocation(ppc)

  • August 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Dynamic Memory Allocation(ppc) as PDF for free.

More details

  • Words: 929
  • Pages: 13
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); }

4

13-03-03

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

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 Instructions

6

13-03-03

Permanent storage area

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))” Allocates a chunk of memory of total size = n * sizeof(int) Returns pointer to first byte

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
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
9

13-03-03

P.P.Chakrabarti, IIT Kharagpur

Arrays of pointers

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

#define N 20 #define M 10 main() { char word[N], *w[M]; int i, n; scanf("%d",&n); for (i=0; i
10

13-03-03

P.P.Chakrabarti, IIT Kharagpur

How it will look like w 0

T

e

n

d

u

l

1

S o

u

r

a

v \0

2

K h

a

n \0

3

I

d

i

n

k

a

r

\0

a \0

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
12

13-03-03

[ppchak]$ ./a.out 5 India Australia Kenya NewZealand SriLanka w[0] = India w[1] = Australia w[2] = Kenya w[3] = NewZealand w[4] = SriLanka

P.P.Chakrabarti, IIT Kharagpur

How this will look like w

13

0

I

n

d

i

a \0

1

A u

s

t

r

2

K e

n

y

a \0

3 4

N e w Z

e

S

a

r

13-03-03

i

L

a

l

i

a \0

a

l

a

n

n

k

a \0

d \0

P.P.Chakrabarti, IIT Kharagpur

Related Documents

Dynamic Memory
November 2019 3
Dynamic
June 2020 20
Dynamic
June 2020 27