1 / 34

ecs30 Winter 2012: Programming and Problem Solving # 22: Dynamic Memory Allocation

ecs30 Winter 2012: Programming and Problem Solving # 22: Dynamic Memory Allocation. Dr. Jeff Rowe Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. malloc & free. malloc: Input: the size of the memory cells

uriah
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 22: Dynamic Memory Allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ecs30 Winter 2012:Programming and Problem Solving#22: Dynamic Memory Allocation Dr. Jeff Rowe Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #22 (guest lecture)

  2. malloc & free • malloc: • Input: the size of the memory cells • Output: the address of the first memory cell ecs30 Winter 2012 Lecture #22 (guest lecture)

  3. malloc & free • malloc: • Input: the size of the memory cells • return: the address of the first memory cell #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); ecs30 Winter 2012 Lecture #22 (guest lecture)

  4. malloc & free • free: • Input: an address • return: nothing #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); free(xp); ecs30 Winter 2012 Lecture #22 (guest lecture)

  5. ecs30 Winter 2012 Lecture #22 (guest lecture)

  6. int *ip; ip = (int *) malloc(sizeof(int));; ip ecs30 Winter 2012 Lecture #22 (guest lecture)

  7. int *ip; ip = (int *) malloc(sizeof(int)); ip ecs30 Winter 2012 Lecture #22 (guest lecture)

  8. int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30 Winter 2012 Lecture #22 (guest lecture)

  9. planet_t *ptp; ptp = (planet_t *) malloc(sizeof(planet_t)); ecs30 Winter 2012 Lecture #22 (guest lecture)

  10. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); ecs30 Winter 2012 Lecture #22 (guest lecture)

  11. NAME strdup -- save a copy of a stringLIBRARY Standard C Library (libc, -lc)SYNOPSIS #include <string.h> char * strdup(const char *s1);DESCRIPTION The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM. ecs30 Winter 2012 Lecture #22 (guest lecture)

  12. #include <stdio.h> #include <string.h> int main(void) { char s1[9] = “UC “; char s2[9] = “Davis”; fprintf(stdout, “%s\n”, strcpy(s1, s2)); return 0; } ecs30 Winter 2012 Lecture #22 (guest lecture)

  13. ‘U’ ‘C’ ‘ ’ ‘\0’ ? ? ? ? ? s1 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s1 strcpy(s1, s2); ecs30 Winter 2012 Lecture #22 (guest lecture)

  14. #include <stdio.h> #include <string.h> int main(void) { char *s1p; char s2[9] = “Davis”; s1p = strdup(s2); fprintf(stdout, “%s\n”, s1p); return 0; } ecs30 Winter 2012 Lecture #22 (guest lecture)

  15. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ecs30 Winter 2012 Lecture #22 (guest lecture)

  16. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ? ? ? ? ? ? ecs30 Winter 2012 Lecture #22 (guest lecture)

  17. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ecs30 Winter 2012 Lecture #22 (guest lecture)

  18. char * strdup(char *s) { … } NAME strdup -- save a copy of a stringLIBRARY Standard C Library (libc, -lc)SYNOPSIS #include <string.h> char * strdup(const char *s1);DESCRIPTION The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM. ecs30 Winter 2012 Lecture #22 (guest lecture)

  19. char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30 Winter 2012 Lecture #22 (guest lecture)

  20. Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30 Winter 2012 Lecture #22 (guest lecture)

  21. Linked-List struct ecs30 Winter 2012 Lecture #22 (guest lecture)

  22. Linked-List struct struct struct ecs30 Winter 2012 Lecture #22 (guest lecture)

  23. Linked-List struct struct struct NULL NULL ecs30 Winter 2012 Lecture #22 (guest lecture)

  24. Linked-List struct struct struct NULL NULL struct ecs30 Winter 2012 Lecture #22 (guest lecture)

  25. Linked-List struct struct struct NULL NULL struct ecs30 Winter 2012 Lecture #22 (guest lecture)

  26. Linked-List struct struct NULL NULL ecs30 Winter 2012 Lecture #22 (guest lecture)

  27. struct ll_element { // single link struct ll_element *next; // data, content … }; ecs30 Winter 2012 Lecture #22 (guest lecture)

  28. struct ll_element { // double links struct ll_element *previous; struct ll_element *next; // data, content … }; ecs30 Winter 2012 Lecture #22 (guest lecture)

  29. struct ll_element { // double links struct ll_element *previous; struct ll_element *next; // data, content void *data; // operations int (*_init)(void *); int (*_print)(void); int (*_find)(void *); }; ecs30 Winter 2012 Lecture #22 (guest lecture)

  30. struct ll_element *llp = head_llp; while (llp != NULL) { (*(llp->print))(); // printing data llp = llp->next; } ecs30 Winter 2012 Lecture #22 (guest lecture)

  31. #define STRSIZ 10 typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet[12]; fwrite(current_planet, sizeof(planet_t), 12, planetFile); fread(current_planet, sizeof(planet_t), 12, planetFile); ecs30 Winter 2012 Lecture #22 (guest lecture)

  32. ecs30 Winter 2012 Lecture #22 (guest lecture)

  33. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; } planet_t; sizeof(planet_t) == ? ecs30 Winter 2012 Lecture #22 (guest lecture)

  34. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; char tail2; } planet_t; sizeof(planet_t) == ? ecs30 Winter 2012 Lecture #22 (guest lecture)

More Related