540 likes | 642 Views
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
E N D
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
malloc & free • malloc: • Input: the size of the memory cells • Output: the address of the first memory cell ecs30b Fall 2008 Lecture #27
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
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
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
planet_t *ptp; ptp = (planet_t *) malloc(sizeof(planet_t)); ecs30 Winter 2012 Lecture #22 (guest lecture)
#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)
Memory Cell Layout main Scan_address Scanf Malloc/free a.out ecs30 Winter 2012 Lecture #22 (guest lecture)
variables • Static/global variables • static int count; • Local variables • Malloc/free ecs30 Winter 2012 Lecture #22 (guest lecture)
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
int *ip; ip = (int *) malloc(sizeof(int));; ip ecs30b Fall 2008 Lecture #27
int *ip; ip = (int *) malloc(sizeof(int)); ip ecs30b Fall 2008 Lecture #27
int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30b Fall 2008 Lecture #27
planet_t *ptp; ptp = (planet_t *) malloc(sizeof(planet_t)); ecs30b Fall 2008 Lecture #27
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
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
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
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)
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)
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()
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)
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)
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
char * strdup(char *) ecs30 Winter 2012 Lecture #22 (guest lecture)
#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
‘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
#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
s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ecs30b Fall 2008 Lecture #27
s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ? ? ? ? ? ? ecs30b Fall 2008 Lecture #27
s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ecs30b Fall 2008 Lecture #27
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
char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30b Fall 2008 Lecture #27
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
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
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
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
Standard File I/O stdout stdin Program stderr foo bar ecs30 Winter 2012 Lecture #11
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
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
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
Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30b Fall 2008 Lecture #27
Linked-List struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27
Linked-List struct struct NULL NULL ecs30b Fall 2008 Lecture #27