Download presentation
Presentation is loading. Please wait.
1
Pointers and dynamic memory
2
malloc, calloc, realloc, free
Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc realloc free - deallocate block of memory Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
3
malloc, calloc, realloc, free
Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int void *p = malloc(4) print p //200
4
malloc, calloc, realloc, free
Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int int *p = (int*) malloc(3*sizeof(int)) print p //200 *p = 2 *(p+1) = 4 *(p+2) = 5
5
malloc, calloc, realloc, free
Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 Allocate block of memory malloc - void* malloc(size_t size) //unsigned int int *p = (int*) malloc(3*sizeof(int)) print p //200 p[0] = 2 p[1] = 4 p[2] = 5
6
malloc, calloc, realloc, free
Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc - void* calloc(size_t num, size_t size) int *p = (int*) calloc(3, sizeof(int)) Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
7
malloc, calloc, realloc, free
Allocate block of memory malloc - void* malloc(size_t size) //unsigned int calloc - void* calloc(size_t num, size_t size) realloc - void* realloc(void *ptr, size_t size) Memory (heap) 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
8
#include<stdio. h> #include<stdlib
#include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int A[n]; for (i = 0; i < n; i++) A[i] = 1+i; printf("the value is %d\n", A[i]); }
9
malloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; printf("%d ", A[i]); }
10
calloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) calloc(n,sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; printf("%d ", A[i]); }
11
calloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) calloc(n,sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) printf("%d ", A[i]); }
12
free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); printf("%d ", A[i]); }
13
free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; //free(A); printf("%d ", A[i]); }
14
free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); A[3] = 9; printf("%d ", A[i]); }
15
free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; free(A); A = NULL; //Adjust pointer to NULL after free printf("%d ", A[i]); }
16
realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 2*n*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); for (i = 0; i < 2*n; i++) printf("%d\n", B[i]); }
17
realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 2*n*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); for (i = 0; i < 2*n; i++) printf("%d\n", B[i]); }
18
realloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, (n/2)*sizeof(int)); printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }
19
realloc as free #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(A, 0); //equivalent to free(A) printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }
20
realloc as malloc #include<stdio.h> #include<stdlib.h> int main() { int n, i; printf("Enter size of array\n"); scanf("%d",&n); int *A = (int*) malloc(n*sizeof(int)); //dynamically allocated array for (i = 0; i < n; i++) A[i] = 1+i; int *B = (int*) realloc(NULL, n*sizeof(int)); //equivalent to malloc printf("Previous block address = %p, new address = %p\n", A,B); printf("%d\n", B[i]); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.