CS 11 C track: lecture 6

Similar documents
CS 11 C track: lecture 5

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CS24 Week 2 Lecture 1

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Lecture 07 Debugging Programs with GDB

Procedural programming with C

CSE 351. GDB Introduction

EL2310 Scientific Programming

Class Information ANNOUCEMENTS

Here's how you declare a function that returns a pointer to a character:

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Exercise Session 6 Computer Architecture and Systems Programming

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

malloc(), calloc(), realloc(), and free()

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Debugging Techniques. CEFET Engineering Week

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CSE 374 Programming Concepts & Tools

Libgdb. Version 0.3 Oct Thomas Lord

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

We first learn one useful option of gcc. Copy the following C source file to your

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Using a debugger. Segmentation fault? GDB to the rescue!

Quiz 0 Review Session. October 13th, 2014

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Programs in memory. The layout of memory is roughly:

Midterm Review. CS 211 Fall 2018

Dynamic Data Structures (II)

BBM 201 DATA STRUCTURES

CS61, Fall 2012 Section 2 Notes

CS 61c: Great Ideas in Computer Architecture

Principles of C and Memory Management

Laboratory 1 Semester 1 11/12

18-600: Recitation #3

CSC C69: OPERATING SYSTEMS

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Data Representation and Storage. Some definitions (in C)

Basic functions of a debugger

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS201- Introduction to Programming Current Quizzes

OpenACC Course. Office Hour #2 Q&A

Programming Studio #9 ECE 190

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

MPATE-GE 2618: C Programming for Music Technology. Syllabus

REFERENCES, POINTERS AND STRUCTS

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

The Dynamic Debugger gdb

Lecture Notes on Memory Management

6.S096: Introduction to C/C++

CS 314 Principles of Programming Languages. Lecture 9

Fall 2018 Discussion 2: September 3, 2018

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Programming Tips for CS758/858

C for C++ Programmers

CS 237 Meeting 18 10/22/12

CCPS 305: Quick guide to C programming for Java folks

Pointers, Dynamic Data, and Reference Types

CSE 410: Systems Programming

SYSC 2006 C Winter 2012

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C Program Development and Debugging under Unix SEEM 3460

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

DAY 4. CS3600, Northeastern University. Alan Mislove

CSE 124 Discussion (10/3) C/C++ Basics

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

System Assertions. Your Submissions. Oral Exams FEBRUARY FEBRUARY FEBRUARY

CSE 333 Midterm Exam Sample Solution 7/29/13

COMP 2355 Introduction to Systems Programming

System Assertions. Andreas Zeller

ELEC 377 C Programming Tutorial. ELEC Operating Systems

FORM 2 (Please put your name and form # on the scantron!!!!)

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Intro to C: Pointers and Arrays

However, in C we can group related variables together into something called a struct.

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Reviewing gcc, make, gdb, and Linux Editors 1

Dynamic memory allocation

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

CS61C : Machine Structures

Chapter 1 Getting Started

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Transcription:

CS 11 C track: lecture 6 Last week: pointer arithmetic This week: The gdb program struct typedef linked lists

gdb for debugging (1) gdb: the Gnu DeBugger http://courses.cms.caltech.edu/cs11/material /c/mike/misc/gdb.html Use when program crashes e.g. from a segmentation violation or when want to walk through execution of program line-by-line

gdb for debugging (2) Before using gdb: Must compile C code with additional flag: -g This puts all the source code into the binary executable Then can execute as: gdb myprogram Brings up an interpreted environment

gdb for debugging (3) gdb> run Program runs... If all is well, program exits successfully, returning you to prompt If there is (e.g.) a crash, gdb will tell you and abort the program

gdb for debugging (4) If your program needs command-line arguments, e.g. myprogram 1 2 3, then you should do this in gdb: gdb> run 1 2 3 This will run myprogram with the command-line arguments 1, 2, and 3

gdb basic commands (1) Stack backtrace ("where") Your program crashes Where was the last line in the program that was executed before the crash? That's what the where command tells you

gdb basic commands (2) gdb> where #0 0x4006cb26 in free () from /lib/libc.so.6 #1 0x4006ca0d in free () from /lib/libc.so.6 #2 0x8048951 in board_updater (array=0x8049bd0, ncells=2) at 1dCA2.c:148 #3 0x80486be in main (argc=3, argv=0xbffff7b4) at 1dCA2.c:44 #4 0x40035a52 in libc_start_main () from /lib/libc.so.6 stack backtrace last call last call in your code

gdb basic commands (3) Look for topmost location in stack backtrace that corresponds to your code Watch out for freeing memory you didn't allocate accessing arrays beyond their maximum elements dereferencing pointers that don't point to part of a malloc()ed block

gdb basic commands (4) break, continue, next, step commands break causes execution to stop on a given line gdb> break foo.c: 100 (setting a breakpoint) continue resumes execution from that point next executes the next line, then stops step executes the next statement goes into functions if necessary (next doesn't)

gdb basic commands (5) print and display commands print prints the value of any program expression gdb> print i $1 = 100 display prints a particular value every time execution stops gdb> display i

gdb printing arrays (1) print will print arrays as well int arr[] = { 1, 2, 3 }; gdb> print arr $1 = {1, 2, 3} N.B. the $1 is just a name for the result print $1 $2 = {1, 2, 3}

gdb printing arrays (2) print has problems with dynamically-allocated arrays int *arr; arr = (int *)malloc(3 * sizeof(int)); arr[0] = 1; arr[1] = 2; arr[2] = 3; gdb> print arr $1 = (int *) 0x8094610 Not very useful...

gdb printing arrays (3) Can print this array by using @ (gdb special syntax) int *arr; arr = (int *)malloc(3 * sizeof(int)); arr[0] = 1; arr[1] = 2; arr[2] = 3; gdb> print *arr@3 $2 = {1, 2, 3}

gdb abbreviations Common gdb commands have abbreviations p (same as print) c (same as continue) n (same as next) s (same as step) More convenient to use when interactively debugging

structs (1) Way to package primitive data objects into an aggregate data object struct declaration: struct point { int x; int y; double dist; /* from origin */ }; /* MUST have semicolon! */

structs (2) struct declaration usually done outside of function, like a function prototype Create/initialize struct like this: struct point p; p.x = 0; /* "dot syntax" */ p.y = 0; p.dist = sqrt(p.x*p.x + p.y*p.y);

structs (3) Using a struct: void foo(void) { struct point p; p.x = 10; p.y = -3; p.dist = sqrt(p.x*p.x + p.y*p.y); /* do stuff with p */ }

structs (4) Using malloc() with structs: struct point *make_point(void) { struct point *p; p = (struct point *) malloc(sizeof(struct point)); return p; } /* free struct elsewhere */

structs (5) Using pointers to structs : void init_point(struct point *p) { (*p).x = (*p).y = 0; (*p).dist = 0.0; /* syntactic sugar: */ p->x = p->y = 0; p->dist = 0.0; }

structs (6) structs can contain arrays or other structs Usually use pointers to structs instead of just plain structs struct foo { int x; struct point p1; /* Unusual */ struct point *p2; /* Typical */ };

structs (7) structs can be "recursive": struct node { int value; struct node *next; }; but can't have struct node next inside declaration (why?)

typedef (1) Typing struct point all the time is tedious Use a typedef (type alias): original type new name typedef struct point Point; typedef int Length; Original type comes first New name is at the end

typedef (2) Type component of typedef can also be a struct typedef struct { /* no name for struct */ int x; int y; double dist; } Point; Point p1, p2; /* no "struct" */ N.B. This is an anonymous struct

typedef (3) Recursively defined structs: typedef struct _node { int value; struct _node *next; } node;

typedef (4) Read this as: typedef struct _node { int value; struct _node *next; } node;

Linked lists node is the linked list struct! Set next pointer to next node in list If next is NULL, then at end of list Linked lists are just chains of nodes

Creating a linked list (1) node *list, *n, *prev;

Linked list (diagram) list n prev

Creating a linked list (2) n = (node *)malloc(sizeof); list = n; /* list points to first node */ n->value = 10; prev = n; /* pointer to previous node */

Linked list (diagram) list n prev

Linked list (diagram) list n prev

Linked list (diagram) list n prev

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next:

Creating a linked list (3) n = (node *)malloc(sizeof); prev->next = n; /* connect nodes */ prev = n; n->value = 20; /*... continued on next slide... */

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Creating a linked list (4) /* Continued... */ n = (node *) malloc(sizeof); prev->next = n; prev = n; n->value = 30; n->next = NULL; /* End of list marker. */

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next: value: 30 next:

Linked list (diagram) list n prev value: 10 next: value: 20 next: value: 30 next: NULL

Linked list (final diagram) list value: 10 next: value: 20 next: value: 30 next: NULL

Creating a linked list (5) Can also create linked lists from the end back to the front Actually easier to do it that way when possible example: lab 6 command-line arguments End-of-list is represented as NULL pointer add nodes to previous list (or to NULL)

Creating a linked list (6) list = NULL; /* Empty list. */ node *n = (node *) malloc(sizeof); n->value = 30; n->next = list; list = n; /* now 1-node list */

Linked list (diagram) list NULL

Linked list (diagram) n list NULL

Linked list (diagram) n value: 30 list NULL

Linked list (diagram) n list NULL value: 30 next: NULL

Linked list (diagram) n list value: 30 next: NULL

Linked list (diagram) list value: 30 n next: NULL

Creating a linked list (7) node *n = (node *) malloc(sizeof); n->value = 20; n->next = list; list = n; /* now 2-node list */

Linked list (diagram) list value: 30 n next: NULL

Linked list (diagram) n list value: 30 next: NULL

Linked list (diagram) n list value: 20 value: 30 next: NULL

Linked list (diagram) n list value: 20 next: value: 30 next: NULL

Linked list (diagram) list n value: 20 next: value: 30 next: NULL

Creating a linked list (8) node *n = (node *) malloc(sizeof); n->value = 10; n->next = list; list = n; /* now 3-node list */

Linked list (diagram) list n value: 20 next: value: 30 next: NULL

Linked list (diagram) n list value: 20 next: value: 30 next: NULL

Linked list (diagram) n value: 10 list value: 20 next: value: 30 next: NULL

Linked list (diagram) n value: 10 next: list value: 20 next: value: 30 next: NULL

Linked list (diagram) n list value: 10 next: value: 20 next: value: 30 next: NULL

Linked list (final diagram) list value: 10 next: value: 20 next: value: 30 next: NULL

Checking malloc() Previous code simplified to fit on slide Actually should check every malloc call for failure n = (node *)malloc(sizeof); if (n == NULL) { } fprintf(stderr, "Error: out of memory.\n"); exit(1);

Iterating through a linked list Standard idiom for going through linked lists: node *n; /* Set all node values to zero. */ for (n = list; n!= NULL; n = n->next) { n->value = 0; } You should be able to figure out how this works

Lab 6 This week's lab: New sorting algorithm: "quicksort" More efficient than ME sort, bubblesort Use on linked lists, not arrays Memory management will be a challenge!

Next time Hash tables More "fun" with pointers ;;-)