Understanding Memory Allocation in C Programming

Slide Note
Embed
Share

This content provides insights into memory allocation in C programming. It covers concepts like malloc, calloc, free, and realloc, illustrating how memory can be dynamically allocated and deallocated. The code snippets demonstrate how to work with arrays and pointers, along with explanations on stack and heap memory. The slides present a visual guide to understanding these memory management techniques.


Uploaded on Aug 05, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. : - 1

  2. " " , : int main() { int a[10]; ... }; 2

  3. int main() { ... printf("enter number of elements\n"); scanf("%d", &size); int a[size]; ... }; 3

  4. , " ( " heap ) 4

  5. Data data ( Stack ) Last In First Out (LIFO) Heap ) ( )! " stack ) ( heap ( ) ( 5

  6. C C malloc calloc free realloc - 0 stdlib.h 6

  7. malloc void *malloc(int nBytes) ) ( NULL 7

  8. malloc int 10 int main() { int *a = (int*) malloc(10 * sizeof(int)); ... return 0; } ) sizeof ( void* ) ( 8

  9. malloc type *var-name = (type*) malloc(amount * sizeof(type)); casting ) ( 9

  10. free void free(void *ptr) ptr - malloc, calloc - realloc ptr - 10

  11. int main() { int *a, size, i; printf("Enter array size\n"); scanf("%d", &size); a = (int*) malloc(size * sizeof(int)); if (a == NULL) return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); free(a); return 0; } 11

  12. int main() { int *a, size, i; main a size i Stack printf("Enter array size\n"); scanf("%d", &size); a = (int*) malloc(size * sizeof(int)); if (a == NULL) return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); Heap free(a); return 0; } 12

  13. void* calloc( unsigned int n, unsigned int size_el ) size_el n ( , , .) void* realloc( void *ptr, unsigned int size ) size NULL , 13

  14. calloc - int main() { int *a, size, i; printf("Enter array size\n"); scanf("%d", &size); a = (int*) calloc(size, sizeof(int)); if (a == NULL) return 1; size , sizeof(int) - 0 for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); free(a); return 0; } 14

  15. realloc - int *ptr = NULL; int size = 0, new_size = 0; scanf("%d", &size); ptr = (int*) malloc( size * sizeof(int) ); /* incomplete, must check if allocation succeeded */ ... scanf("%d", &new_size); ptr = (int*) realloc( ptr, new_size*sizeof(int) ); /* incomplete, must check if allocation succeeded */ ... if (ptr != NULL) free(ptr); 15

  16. int* func() { int *memPtr = NULL; memPtr = (int*) malloc(10 * sizeof(int)); ... return memPtr; } memPtr int main() { int * ptr = func(); if (ptr != NULL) { // do something with ptr free(ptr); ptr = NULL; } } ptr 16

  17. ' : " " ' : , ' ' 17

  18. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 18

  19. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 19

  20. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 20

  21. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 21

  22. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } ? 22

  23. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 23

  24. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 24

  25. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } capacity 25

  26. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 26

  27. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } \0 27

  28. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } ( ) 28

  29. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 29

  30. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 30

  31. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 31

  32. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 32

  33. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); , free(line); return 0; } 33

  34. - : NULL exit -

  35. exit void exit(int status); stdlib.h

  36. exit - realloc int , void resize(int *array, int newSize) { if (array == NULL) return; array = (int*) realloc(array, newSize * sizeof(int)); if (array == NULL) { printf("Fatal error: memory allocation failed!\n"); exit(1); } } 36

  37. typedef struct { char *title, *author; } Book; Book* newBook(const char *title, const char *author) { Book *result = (Book*) malloc(sizeof(Book)); /* incomplete, must check if allocation succeeded */ result->title = strdup(title); result->author = strdup(author); /* incomplete, must check if allocation succeeded */ return result; } 37

  38. 38

  39. Command Line Arguments main - ) ( int main(int argc, char *argv[]) { } 39

  40. ? ) - main ( : argc argv[argc] == NULL argv[0] 40

  41. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i; for (i = 0; i < argc; i++) printf("argv[%d] = %s\n", i, argv[i]); return EXIT_SUCCESS; } 41

  42. 42

  43. NULL hello world argc 100 4 abcde argv 1 43

  44. int main(int argc, char *argv[]) { double leftOperand, rightOperand, result; char operator; /* incomplete, make sure all arguments are present */ leftOperand = atof(argv[1]); rightOperand = atof(argv[3]); operator = *argv[2]; switch (operator) { case '+': result = leftOperand + rightOperand; break; case '-': result = leftOperand - rightOperand; break; case '*': result = leftOperand * rightOperand; break; case '/': result = leftOperand / rightOperand; break; } printf("%g %c %g = %g\n", leftOperand, operator, rightOperand, result); return EXIT_SUCCESS; } 44

More Related Content