Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and dynamic memory

Similar presentations


Presentation on theme: "Pointers and dynamic memory"— Presentation transcript:

1 Pointers and dynamic memory

2 What if we don’t know how much memory will be required to store our data?

3 It’s possible to determine the amount of memory needed at the execution time and then request this from the operating system. This is called dynamic memory allocation.

4 Pointers and dynamic memory
#include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

5 Pointers and dynamic memory
Stack Play sq() x Pause sos() a,b,c main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

6 Pointers and dynamic memory
Stack Play sos() a,b,c Pause main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

7 Pointers and dynamic memory
Stack Play main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

8 Pointers and dynamic memory
Stack Play printf() Pause main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

9 Pointers and dynamic memory
Stack Play main() Stack frame m,n Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

10 Pointers and dynamic memory
Stack Stack frame Global total #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

11 Pointers and dynamic memory
Stack Stack frame Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

12 Pointers and dynamic memory
Stack (1 MB)  D()  Stack overflow C() B() Stack frame A() Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

13 Pointers and dynamic memory
Stack Stack frame Global #include<stdio.h> int total; int square(int a) { return a*a; //a^2 } int squareofsum(int a, int b) int c = square(a+b); return c; //(a+b)^2 int main() int m = 2, n = 4; total = squareofsum(m,n); printf("Square of sum is %d\n",total); Application's memory  Free store Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

14 Pointers and dynamic memory
Stack Stack frame Global Heap -- Not heap date structure To access heap – malloc() calloac() realloc() free() Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text)

15 Pointers and dynamic memory
Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; *p = 20; } Stack Heap 20 800 main() p = 10 400 x

16 Pointers and dynamic memory
Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; free(p); *p = 20; } Stack Heap 20 800 main() p = 400 x

17 Pointers and dynamic memory
Application's memory Heap function calls, local variables Stack global Static/Global Instructions Code (Text) #include<stdio.h> #include<stdlib.h> int main() { int x; //will be stored in the stack int *p; p = (int*)malloc(sizeof(int)); *p = 10; free(p); p = (int*)malloc(10*sizeof(int)); *p = 20; } Stack Heap 200 main() p = 200 240 x p[0], p[1], p[2] *p, *(p+1), *(p+2)

18 Dynamic memory allocation - Pros
No need to decide how much memory to allocate when writing the program, giving us more flexibility. Free up memory for other programs after it is no longer used by the program which allocates it.

19 Dynamic memory allocation - Cons
Have to use pointers. The programmer has the responsibility of tracking the locations of allocated memory and freeing it up. It’s possible to create a memory leak, i.e., memory that is no longer used is not freed. If the process is repeated, the amount of unfreed memory grows in size.

20 Example 1 int i, *d; d = (int *) malloc( 5 * sizeof(int) ); for(i = 0; i < 5; i++) d[i] = i + 100;


Download ppt "Pointers and dynamic memory"

Similar presentations


Ads by Google