10-more_linked_structures

10-more_linked_structures - Computer Science 136 David R...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 10 – Linked Structures, Racket We look more deeply at pointers and linked structures in C. We also look at lists in Racket and consider how they are implemented. CS 136 – Winter 2012 10 – Linked Structures, Racket 1/28 Racket vs. C C forces you to do a lot of work You want lists? You need to implement them yourself Racket is much more full-featured It’s not magic! The implementation is generally hidden (at least in the student languages) Studying Racket and C together gives a more complete view of what your Racket code is doing CS 136 – Winter 2012 10 – Linked Structures, Racket 2/28 Review: Pointers What does this code print? File: pointers1.c #include <stdio.h> #include <stdlib.h> int main( void ) { int *p = malloc( sizeof ( int )); *p = 42; printf("%p %d %p\n",p,*p,&p); free(p); } 0x4197028 42 0xbed105bc Only an example: your program will produce different output (possibly each time you run it) CS 136 – Winter 2012 10 – Linked Structures, Racket 3/28 Review: Pointers How about this code? File: pointers2.c #include <stdio.h> #include <stdlib.h> int main( void ) { int *p = malloc( sizeof ( int )); *p = 42; free(p); printf("%p %d %p\n",p,*p,&p); } After free(p); pointer p has become a dangling reference runC will tell you “ Invalid read ” CS 136 – Winter 2012 10 – Linked Structures, Racket 4/28 Review: Pointers How about this code? File: pointers3.c #include <stdio.h> #include <stdlib.h> int main( void ) { int *p = malloc( sizeof ( int )); free(p); *p = 42; printf("%p %d %p\n",p,*p,&p); } Again, after free(p); pointer p has become a dangling reference runC will tell you “ Invalid write ” and “ Invalid read ” CS 136 – Winter 2012 10 – Linked Structures, Racket 5/28 Review: Pointers How about this code? File: pointers4.c #include <stdio.h> #include <stdlib.h> int main( void ) { int *p; *p = 42; printf("%p %d %p\n",p,*p,&p); } Pointer p is uninitialized Probably will get a Segmentation fault runC will tell you: “ Use of uninitialised value of size 4 ” CS 136 – Winter 2012 10 – Linked Structures, Racket 6/28 Review: Pointers How about this code? File: pointers5.c #include <stdio.h> #include <stdlib.h> int *p; int main( void ) { *p = 42; printf("%p %d %p\n",p,*p,&p); } Pointer p in the static area is initialized to 0 0 is the same as NULL Will give a Segmentation fault (can’t write location 0) runC will tell you = Address 0x0 is not stack’d, malloc’d or (recently) free’d CS 136 – Winter 2012 10 – Linked Structures, Racket 7/28 Last time: CardList ADT Last class we defined a CardList ADT: typedef struct cardlist_struct * CardList; CardList drawKCards ( int k , int n); void printCardList (CardList first); void freeCardList (CardList first); Internally represented by a linked structure: CardList cl = drawKCards(3,10); // some line of code......
View Full Document

This note was uploaded on 02/22/2012 for the course CS cs136 taught by Professor Cormack during the Winter '10 term at Waterloo.

Page1 / 28

10-more_linked_structures - Computer Science 136 David R...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online