1 / 8

5.6 Pointer Arrays; Pointers to Pointers

Second Idea. d e j k a b c . d e j k a b c . de. de. jk. jk. abc. abc. Complete solution See pp. 107-110. 5.6 Pointer Arrays; Pointers to Pointers. Since pointers are variables themselves,

wilton
Download Presentation

5.6 Pointer Arrays; Pointers to Pointers

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. Second Idea d e \0 j k \0 a b c \0 d e \0 j k \0 a b c \0 de\0 de\0 jk\0 jk\0 abc\0 abc\0 • Complete solution • See pp. 107-110 5.6 Pointer Arrays; Pointers to Pointers Since pointers are variables themselves, they can be stored in arrays just as other variables can. Example: Design a program that will sort a set of text lines into alphabetic order. First Idea • The sorting process has three steps • Read all the lines of input • Sort them • Print them in order Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 7 - 8 April 2008

  2. Memory Analysis We will analyze the state of the memory at different steps during the evaluation of the expression (nlines = readlines(lineptr, MAXLINES) of the main program. We will assume that the constants MAXLINES, MAXLEN and ALLOCSIZE have resp. the values 4, 6 and 10 and that the input contains the three lines: de jk abc The steps are : 1. At the end of main()'s initialization 2. At the end of the initialization of the local variables of the first call of the function readlines(lineptr,MAXLINES) 3. At the end of the first evaluation of (len = getline(line,MAXLINES)) 4. At the end of the first evaluation of (p = alloc(len)) 5. At the end of the first evaluation of lineptr[nlines++] = p 6. Just before quitting readlines(lineptr, MAXLINES)

  3. "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf allocbuf[0] Main preprocessing operations allocbuf[1] lineptr[0] #define MAXLINES 4 #define MAXLEN 6 #define ALLOCSIZE 10 allocbuf[2] lineptr[1] lineptr[2] allocbuf[3] lineptr[3] allocbuf[4] nlines Main runtime operations allocbuf[5] - main()'s global variables declaration and init. allocbuf[6] char *lineptr[4]; allocbuf[7] - main()'s local variables declaration and init. allocbuf[8] int nlines; allocbuf[9] - alloc() and afree()'s private var. decl. and init. allocp static char *allocbuf[10]; static char *allocp = allocbuf; Variables of main() Private variables of alloc() and afree() 1. At the end of main()'s initialization

  4. "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf 4 allocbuf[0] maxlines allocbuf[1] lineptr lineptr[0] allocbuf[2] lineptr[1] len lineptr[2] allocbuf[3] nlines line lineptr[3] allocbuf[4] p nlines allocbuf[5] line[0] allocbuf[6] line[1] allocbuf[7] line[2] line[3] allocbuf[8] readlines()'s formal parameters allocbuf[9] line[4] "readlines.maxlines = 4;" "readlines.lineptr = lineptr;" allocp line[5] readlines()'s local parameters int len, nlines; char *p, line[6]; Variables of main() Private variables of alloc() and afree() Local variables of readlines 2. At the end of the initialization of the local variables of the first call of the function readlines(lineptr,MAXLINES)

  5. "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf 4 allocbuf[0] maxlines allocbuf[1] lineptr lineptr[0] allocbuf[2] 3 lineptr[1] len 0 lineptr[2] allocbuf[3] nlines line lineptr[3] allocbuf[4] p nlines 'd' allocbuf[5] line[0] 'e' allocbuf[6] line[1] '\n' allocbuf[7] line[2] line[3] '\0' allocbuf[8] Main operations in readlines() allocbuf[9] line[4] nlines = 0; allocp line[5] Main operations in getline() - "read next line" - "return length of the readed line" Variables of main() Private variables of alloc() and afree() Local variables of readlines 3. At the end of the first evaluation of (len = getline(line,MAXLINES))

  6. Main operations in alloc() allocp += len; return allocp - len; "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf 4 4 allocbuf[0] maxlines MAXLINES allocbuf[1] lineptr lineptr[0] allocbuf[2] 3 lineptr[1] len 0 lineptr[2] allocbuf[3] nlines line lineptr[3] allocbuf[4] p nlines 'd' allocbuf[5] line[0] 'e' allocbuf[6] line[1] '\n' allocbuf[7] line[2] line[3] '\0' allocbuf[8] allocbuf[9] line[4] allocp line[5] Variables of main() Private variables of alloc() and afree() Local variables of readlines 4. At the end of the first evaluation of (p = alloc(len))

  7. 'd' 'e' '\0' '\0' Main operations line[len-1] = '\0'; strcpy(p,line); "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf 4 allocbuf[0] maxlines allocbuf[1] lineptr lineptr[0] allocbuf[2] 3 lineptr[1] len 0 1 lineptr[2] allocbuf[3] nlines line lineptr[3] allocbuf[4] p nlines 'd' allocbuf[5] line[0] 'e' allocbuf[6] line[1] '\n' allocbuf[7] line[2] line[3] '\0' allocbuf[8] allocbuf[9] line[4] allocp line[5] lineptr[nlines++] = p; Variables of main() Private variables of alloc() and afree() Local variables of readlines 5. At the end of the first evaluation of lineptr[nlines++] = p

  8. "MAXLINES = 4" "ALLOCSIZE = 10" "MAXLEN = 6" lineptr allocbuf 'd' 4 allocbuf[0] maxlines 'e' allocbuf[1] lineptr lineptr[0] allocbuf[2] '\0' 4 lineptr[1] len 'j' 3 lineptr[2] allocbuf[3] nlines line lineptr[3] allocbuf[4] 'k' p nlines '\0' 'a' allocbuf[5] line[0] 'b' allocbuf[6] 'a' line[1] 'b' 'c' allocbuf[7] line[2] line[3] 'c' '\0' allocbuf[8] Main operations '\0' allocbuf[9] '\0' line[4] "read the next 2 lines" allocp line[5] Variables of main() Private variables of alloc() and afree() Local variables of readlines 6. Just before quitting readlines(lineptr, MAXLINES)

More Related