Programming Studio #9 ECE 190

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

CS354 gdb Tutorial Written by Chris Feilbach

Class Information ANNOUCEMENTS

Lecture 2: C Programm

Lecture 3: C Programm

CSE 351. GDB Introduction

A Tutorial for ECE 175

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

Lecture 07 Debugging Programs with GDB

Problem Set 1: Unix Commands 1

Pointers, Arrays and Parameters

CSE 374 Programming Concepts & Tools

Today s presentation. Git gdb Project 1


Laboratory 1 Semester 1 11/12

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

Exercise Session 6 Computer Architecture and Systems Programming

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.

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

EL2310 Scientific Programming

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

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

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CSC 2400: Computer Systems. Using the Stack for Function Calls

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Basic functions of a debugger

Functions BCA-105. Few Facts About Functions:

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

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

CS/COE 0449 term 2174 Lab 5: gdb

APS105. Pointers. Memory RAM 11/5/2013. Pointers. Need a way to refer to locations of things. a pointer: Address. Example: In C

Intermediate Programming, Spring 2017*

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Topic 6: A Quick Intro To C

Reviewing gcc, make, gdb, and Linux Editors 1

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

Functions. Arash Rafiey. September 26, 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Data and File Structures Laboratory

Scientific Programming in C IX. Debugging

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Lecture 04 Introduction to pointers

18-600: Recitation #3

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

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

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CS3210: Tutorial Session 2. Kyuhong Park-- edited by Kyle Harrigan

Chapter 1 Getting Started

{C} Tools of the Trade

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

C Pointers. 6th April 2017 Giulio Picierro

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

Chapter 7: User Defined Functions and Stack Mechanics

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

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

1 A Brief Introduction To GDB

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Introduction to Scientific Computing and Problem Solving

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Wednesday, October 15, 14. Functions

Computer Programming

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Module 27 Switch-case statements and Run-time storage management

Assembler Programming. Lecture 10

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

SYSC 2006 C Winter 2012

Lab 3. Pointers Programming Lab (Using C) XU Silei

Function Calls and Stack Allocation

Basic and Practice in Programming Lab7

CS 103 Lab - Party Like A Char Star

Function Calls and Stack Allocation

Stack. Stack. Function Calls and Stack Allocation. Stack Popping Popping. Stack Pushing Pushing. Page 1

Intermediate Programming, Spring 2017*

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

CS 314 Principles of Programming Languages. Lecture 9

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Debugging. P.Dagna, M.Cremonesi. May 2015

FUNCTIONS POINTERS. Pointers. Functions

EE 355 Lab 3 - Algorithms & Control Structures

CS102 Software Engineering Principles

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

ECE 190 Midterm Exam 3 Spring 2011

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

CSCI 2132 Software Development. Lecture 18: Functions

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Subject: Fundamental of Computer Programming 2068

Transcription:

Programming Studio #9 ECE 190

Programming Studio #9 Concepts: Functions review 2D Arrays GDB

Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS.

Functions Same as subroutines Declared like variables: e.g., int func1(); char func2(); Return types Declaration specifies return type (i.e., output) func1() returns an integer func2() returns a character

Function Parameters Functions often take parameters Parameters Ex.: int larger_num(int num1, int num2); num1 and num2 are integers and are passed to the function larger_num, which returns an int Function larger_num then manipulates num1 and num2 to output larger of two Ex.: int a = larger_num(5,6);

Function: larger_num int larger_num( int num1, int num2 ); void main () { int x = 5; } int a = larger_num(x,6); /* return value (6) gets stored in a */ Call Declaration (must declare before call) Implementation larger_num() written outside main() int larger_num(int num1, int num2) { if( (num1-num2) > 0) return num1; else if( (num2 - num1) > 0) return num2; else return num1; /* both numbers are equal, therefore return either */ }

Pass by Value Pass arguments from caller to callee using actual values Value of expression is evaluated and pushed onto run-time stack to pass to the function int main() void Swap(int P, int Q) { { int A=3; int temp = P; int B=4; P = Q; Swap(A, B); Pass by Value } } Q = temp; Fail!

Pass by Reference By Value values not actually swapped Swap needs addresses of A and B to swap Pass by reference actually passes addresses int main() void Swap(int* P, int* Q) { { int A=3; int temp = *P; int B=4; *P = *Q; Swap(&A, &B); *Q = temp; } }

Two-Dimensional Arrays -Declaration: Just like regular arrays! Specify dimensions ex: int balances[11][6]; -Visualizing as a table, a is the row index, and b is the column index -Can be thought of as an array of arrays ex: balances[7] is an array of 6 integers (it is the 8 th array in the array of arrays) -This two-dimensional array must be put into a one-dimensional memory -C uses row-major order 9

Pointer-Array Equivalence Arrays are treated as pointers in most cases int array[5]; int* ptr = array; /* ptr and array can now be used interchangeably! */ The following pairs are equivalent: *a and a[0] a[10] and *(a + 10) These function declarations are equivalent: void func(float * arr); void func(float arr[]);

Using a debugger with C gdb is a debugger that is very similar to lc3sim Let s go through a short exercise to show you how gdb works. First: Download the code wget http://courses.engr.illinois.edu/ece190/discussion/spring11/ps9/test.c gcc g test.c o test Warning: We are intentionally compiling this without the usual flags to demonstrate what can go wrong if you don t. You must have all the required flags when working on mps.

Open up GDB (type gdb <exe> )

Type help to see a list of available commands

Type list to see your code!

Hitting enter runs the previously typed command again in this case it lists more lines.

Type list <num> to list the code around a certain line number (useful later).

Let s just run the code it crashed?

Run the backtrace command (or bt ) to get information about where the program crashed

It crashed at line 11, so let s look at line 11 Looks like we messed up scanf it should have been scanf ("%d", &i);

In order to change our code, we must quit the debugger. Open up test.c in VIM ( vim test.c ) and change line 11. Don t forget to recompile!

Let s open GDB back up ( gdb test ) and we can list the code to see it is fixed now. Always have to quit, fix, recompile, and launch gdb again to squash a bug.

If something is still wrong in your code, you can always set breakpoints to stop your code before that line executes. Type break 11 and run the code. It steps before executing line 11.

Type step to execute just that one line of code. To see the results print i and print num_scan will print out the variable values!

To finish running all of our code, type continue. Now the code will go to the next breakpoint, or finish executing if no breakpoints are left.

Debugging gdb reference Use gdb to debug C Compile using debug flag (i.e., -g) gcc g <C source file> -o <executable name> Run gdb in terminal gdb <executable name> Insert breakpoints break <line number> Run program run Step through program and into functions step Step through program next Continue program continue