. Pointers to functions Debugging. Logistics u Mid-term exam: 18/11  Closed books  List of topics – see web page Some you have to read by yourself!

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
C Programming - Lecture 5
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Debugging: Catching Bugs ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
. Plab – Tirgul 5 pointers to pointers, libraries.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
. Plab – Tirgul 4 structs & arrays, file I/O, debugging memory errors.
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Plab – Tirgul 3 Makefiles, Libraries, Debugging and Common Bugs.
Lecture 3 Interfaces Pointers to Functions Memory bugs, File I/O Variables – the special kind.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Guide To UNIX Using Linux Third Edition
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
Outline Midterm results Static variables Memory model
1 ENERGY 211 / CME 211 Lecture 13 October 20, 2008.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Some Basics && GDB overview Ram Sheshadri –
CSC 230: C and Software Tools Rudra Dutta Computer Science Department Course Introduction.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Problem of the Day  Why are manhole covers round?
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Agenda Attack Lab C Exercises C Conventions C Debugging
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
(language, compilation and debugging) David 09/16/2011.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Debugging of # P. Hristov 04/03/2013. Introduction Difficult problem – The behavior is “random” and depends on the “history” – The debugger doesn’t.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a wrappered function? –How to assess efficiency. –What is a clean interface?
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
C Programming Chapters 11, . . .
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
CSE 451 Section 1 C Refresher (or introduction?) Project 0.
Generic Programming in C
Checking Memory Management
C Basics.
Data Structures and Algorithms
Miscellaneous functions
CSE 403 Lecture 17 Coding.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
When your program crashes
Appendix F C Programming Environment on UNIX Systems
Module 14 Miscellaneous Topics
C Programming - Lecture 5
Dynamic Memory – A Review
Presentation transcript:

. Pointers to functions Debugging

Logistics u Mid-term exam: 18/11  Closed books  List of topics – see web page Some you have to read by yourself! u Reminder: one person per exercise  Copying will be severely punished  Make sure your PLAB directory is not readable for other people

Pointers to Functions u C/C++ allow to have a pointer to a function int foo(int x) {…} main() { int (*func)(int); // func is a pointer to a function func = &foo; func = foo; // same int x = (*func)(7); // same as x = foo(7) }

Example: Numerical Integrator

double numericalIntegration( double a, double b, double (*func)(double), int k ) { double delta = (b - a)/k; double Sum = 0; for( double x = a+0.5*delta; x < b; x+= delta ) Sum += (*func)(x); return Sum*delta; } See NumericalIntergator.c

Example u Suppose we implement an interface of a list of ints IntList.h: struct IntList; // intListNew() // Allocates a new list IntList* intListNew(); …

Example typedef void (*funcInt)( int x, void* Data ); // intListMAPCAR(List, Func, Data ) // // Apply Func to each element of the list // void intListMAPCAR( IntList* List, funcInt Func, void* Data );

Using MAPCAR See UseList.c

“Generic” interface u MAPCAR is a uniform way of performing computations over list elements  The given function provides the different functional element u Pointers to functions provide a way to write code that receives functions as arguments

Example: qsort Library procedure: qsort( void *base, size_t n, size_t size, int (*compare)(void const*, void const *) );  base – start of an array  n – number of elements  size – size of each element  compare – comparison function

Using qsort int compareInt(void const *p, void const *q) { int a = *(int const*)p; int b = *(int const*)q; if( a < b ) return -1; return a > b; } … int array[10] = { … }; qsort( array, 10, sizeof(int), compareInt );

Testing Important aspect of programming u Careful testing of each module, reduce further bugs later on. u Automatic retesting, allow to ensure changes in code do not introduce new “features” How can we test our code?

Testing “Interpreter” u A generic code for interactive/batch test int testCmdLine( TestCmdLineCommand const*Commands, void* Data ) Arguments:  Commands – list of commands  Data – data shared by commands

Testing Interpreter enum TestCmdLineReturnCode { e_TestCmdLineOK, e_TestCmdLineQuit, e_TestCmdLineAbort }; typedef TestCmdLineReturnCode (*TestCmdLineFunc)(void* Data, char const* const* ArgV ); struct TestCmdLineCommand { char* m_Name; // Command Name char* m_Desc; // One line description int m_ArgNum; // # of arguments TestCmdLineFunc m_Func; // Function to call };

Building a test program See TestIntList.c

To do Read man pages  FILE I/O: fopen, fclose, fprintf, fgetchar, fflush  Sorting & Searching: qsort, heapsort, bsearch u Compile program supplied with this class u Run them in DDD (or gdb)  set breakpoints  step through program  force an assert, and see what happens

Debugging Divide & Conquer 2. “Define” the bug --- reproduce it 3. Use tools: debugger & more 4. Don’t panic --- think!

Define the bug Spend the time to find out u What is wrong? u Minimal settings that lead to the error? Reproduce the wrong behavior! u Preferably on a small example

Divide & Conquer Consider possible points of failure  check each one of them separately

Use Debugger Debugger u Allow to monitor run time behavior u Check where the program crashes u Put breakpoints on specific events u Trace execution of the program

Debugger Debugger can save a lot of time u Find why the program crash u Understand the context (call tree, value of variables, etc.) But… u Don’t be trapped into using debuggers all the time

Other tools u Intermediate printouts u self-checking code u asserts u Memory allocation & leaks (Tirgul)

Don’t Panic There a sensible explanation to the bug  Always!  Don’t rush to blame the compiler/OS  Don’t attribute bugs to mysterious forces Do not try random changes to see if they resolve the program  This will only introduce more bugs!