1 / 54

ecs30 Summer 2014: Programming and Problem Solving #10: malloc /free, Linked List

ecs30 Summer 2014: Programming and Problem Solving #10: malloc /free, Linked List. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. malloc & free. malloc: Input: the size of the memory cells

hanne
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving #10: malloc /free, Linked List

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 Summer 2014:Programming and Problem Solving#10: malloc/free, Linked List Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 Lecture #01

  2. malloc & free • malloc: • Input: the size of the memory cells • Output: the address of the first memory cell ecs30b Fall 2008 Lecture #27

  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)); ecs30b Fall 2008 Lecture #27

  4. Planet_t *xp; Xp = (Planet *) malloc(sizeof(planet_t)); Strcpy(xp->name, “Jupiter”); Strcpy((*xp).name, “Jupiter”); Xp->moons = 5; Planet_tpt; Xp = &pt; ecs30 Winter 2012 Lecture #01

  5. planet_t **xpp =NULL; intplanetcount; scanf(“%d”, &planetcount); xpp = (planet **) malloc(sizeof(planet_t) * planetcount); If (xpp != NULL) { fread(xpp, sizeof(planet_t), planetcount, file_ptr); (xpp[3])->moons = 5;} ecs30 Winter 2012 Lecture #01

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

  7. #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)

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

  9. Memory Cell Layout main Scan_address Scanf Malloc/free a.out ecs30 Winter 2012 Lecture #22 (guest lecture)

  10. variables • Static/global variables • static int count; • Local variables • Malloc/free ecs30 Winter 2012 Lecture #22 (guest lecture)

  11. malloc & free • free: • Input: an address • return: nothing #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); free(xp); ecs30b Fall 2008 Lecture #27

  12. ecs30b Fall 2008 Lecture #27

  13. int *ip; ip = (int *) malloc(sizeof(int));; ip ecs30b Fall 2008 Lecture #27

  14. int *ip; ip = (int *) malloc(sizeof(int)); ip ecs30b Fall 2008 Lecture #27

  15. int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30b Fall 2008 Lecture #27

  16. planet_t *ptp; ptp = (planet_t *) malloc(sizeof(planet_t)); ecs30b Fall 2008 Lecture #27

  17. Void ** Int ** Planet_t **xyz_p; Planet_t *abc_p; Planet_t XYZ[8][8]; Xyz_p= XYZ; Abc_p = Xyz_p[3]; ecs30 Winter 2012 Lecture #01

  18. Void ** Int ** Planet_t **xyz_p; Planet_t *abc_p; Xyz_p = malloc(sizeof(planet_t *) * 3); For (I = 0; I < 3; i++) xyz_p[i] = malloc(sizeof(planet_t) * 3); ecs30 Winter 2012 Lecture #01

  19. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); ecs30b Fall 2008 Lecture #27

  20. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); printMatrix(a_ptr); void printMatrix(int a_array[][25]) {…} ecs30 Winter 2012 Lecture #22 (guest lecture)

  21. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); printMatrix(a_ptr); void printMatrix(int **a_ptr) { printf(…,((int [][25]) a_ptr)[i][j]);} ecs30 Winter 2012 Lecture #22 (guest lecture)

  22. ecs30 Winter 2012 Lecture #14

  23. What to do To complete four functions 1. intcountMatrix () // do some sum up 2. intcountNeighbor () //how to denote neighbor? 3. intGOLrule () //if(){} 4. void nextGeneration () //calleGOLrule() and countNeighbor()

  24. 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)

  25. 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)

  26. 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. ecs30b Fall 2008 Lecture #27

  27. char * strdup(char *) ecs30 Winter 2012 Lecture #22 (guest lecture)

  28. #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; } ecs30b Fall 2008 Lecture #27

  29. ‘U’ ‘C’ ‘ ’ ‘\0’ ? ? ? ? ? s1 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s1 strcpy(s1, s2); ecs30b Fall 2008 Lecture #27

  30. #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; } ecs30b Fall 2008 Lecture #27

  31. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ecs30b Fall 2008 Lecture #27

  32. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ? ? ? ? ? ? ecs30b Fall 2008 Lecture #27

  33. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ecs30b Fall 2008 Lecture #27

  34. 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. ecs30b Fall 2008 Lecture #27

  35. char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30b Fall 2008 Lecture #27

  36. Debugging Logic Errors for (i = 1; i < n; ++i) product *= i; Section 5.10 for (i = 1; i < n; ++i) { product *= i; printf(“product=%d, i=%d\n”, product, i); } printf(“OUT:product=%d, I =%d\n”, product, i); ecs30 Winter 2012 Lecture #11

  37. Preprocessor #define FACT_DEBUG … … for (i = 1; i < n; ++i) { product *= i; #ifdef FACT_DEBUG printf(“product=%d, i=%d\n”, product, i); #endif/* FACT_DEBUG */ } #ifdef FACT_DEBUG printf(“OUT:product=%d, I =%d\n”, product, i); #endif /* FACT_DEBUG */ % gcc -c fact.c -DFACT_DEBUG % gcc -c fact.c -DFACT_DEBUG=XYZ ecs30 Winter 2012 Lecture #11

  38. The C “TrIO” • printf, scanf(stdout, stdin) • [printf/scanf](<format string>) • fprintf, fscanf(file pointer) • Chapter 12 • [fprintf/fscanf](<FILE *>, <format string>) • fopen and fclose • sprintf, sscanf(string) • Chapter 9 • [sprintf/sscanf](<char *>, <format string>) ecs30 Winter 2012 Lecture #11

  39. File I/O stdout stdin Program stderr foo scanf(“%d”, &x); fscanf(f, “%d”, &x); sscanf(s, “%d”, &x); bar ecs30 Winter 2012 Lecture #11

  40. Standard File I/O stdout stdin Program stderr foo bar ecs30 Winter 2012 Lecture #11

  41. FILE *out_file_p; FILE *out_debug_file_p; intmain(void) {… /* initialization */ out_file_p = fopen("output.hw6_1", "w"); out_debug_file_ p = fopen("debug.hw6_1", "w"); If (out_file_p == NULL) {exit(-1);} /* using the FILE pointers */ /* regular output example */ fprintf(out_file_p, "...", ...); /* debug output example */ #ifdef ECS30_DEBUG fprintf(out_debug_file_p, "...", ...); #endif /*ECS30_DEBUG */ /* finishing/edning */ fclose(out_file_p); fclose(out_debug_file_p); ecs30 Winter 2012 Lecture #17

  42. GNU debugger • The -g flag • gdb a.out • run (r) • break (b) • next (n), nexti (ni) • step (s) • print (p) • display • continue (c) • watch • x ecs30 Winter 2012 Lecture #16

  43. gdb Examples $ script hw7_1_gdb.script • display i • c • n 2 • ni 9 • s • watch i == 3 • watch i != 0 • x /4 0xbffff862 • p i • p s • p s[14] • p s[12] = ‘\0’ • p i = 0 • b main • b strlen • b 12 http://www.digilife.be/quickreferences/QRC/GDB Quick Reference.pdf http://www.hlrs.de/organization/amt/services/tools/debugger/gdb/doc/gdb-6.3.pdf ecs30 Winter 2012 Lecture #16

  44. Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30b Fall 2008 Lecture #27

  45. Linked-List struct ecs30b Fall 2008 Lecture #27

  46. Linked-List struct struct struct ecs30b Fall 2008 Lecture #27

  47. Linked-List struct struct struct NULL NULL ecs30b Fall 2008 Lecture #27

  48. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  49. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  50. Linked-List struct struct NULL NULL ecs30b Fall 2008 Lecture #27

More Related