Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Utilizing the GDB debugger to analyze programs Background and application.
Hank Childs, University of Oregon April 17 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
C Slides and captured lecture (video and sound) are available at:
Guide To UNIX Using Linux Third Edition
C Slides and captured lecture (video and sound) are available at:
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Computer Science 210 Computer Organization Introduction to C.
Homework Reading Programming Assignments
Hank Childs, University of Oregon April 24 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon April 8th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon April 3rd, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Creating your first C++ program
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Some Basics && GDB overview Ram Sheshadri –
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Hank Childs, University of Oregon May 19 th & 21st, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ /
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.
Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang.
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
C is a high level language (HLL)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hank Childs, University of Oregon April 7 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon May 27 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Hank Childs, University of Oregon April “29 th”, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / /
Lecture 3 Translation.
Last week: We talked about: History of C Compiler for C programming
Hank Childs, University of Oregon
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Computer Science 210 Computer Organization
Compilation and Debugging
Compilation and Debugging
Lab: ssh, scp, gdb, valgrind
Hank Childs, University of Oregon
Hank Childs, University of Oregon
Computer Science 210 Computer Organization
Hank Childs, University of Oregon
Lab: ssh, scp, gdb, valgrind
Chapter 11 Introduction to Programming in C
Getting Started: Developing Code with Cloud9
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Homework Reading Programming Assignments Finish K&R Chapter 1
Hank Childs, University of Oregon
Appendix F C Programming Environment on UNIX Systems
C++ Programming Basics
An Overview of C.
Compiler vs linker The compiler translates one .c file into a .o file
Makefiles, GDB, Valgrind
Presentation transcript:

Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 5: memory errors

Announcements Projects – 1C delayed until Thurs the 14 th – 4A assigned today – 2B assigned on Friday Discussion this week on debuggers/memory checkers No class on Weds April 27 – likely a short YouTube replacement lecture

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

Build: The Actors File types – Source code – Object code – Executable code Programs – Compiler – Linker Source code (e.g., C code) Source code (e.g., C code) Compiler Object code Linker Executable code

Compilers, Object Code, and Linkers Compilers transform source code to object code – Confusing: most compilers also secretly have access to linkers and apply the linker for you. Object code: statements in machine code – not executable – intended to be part of a program Linker: turns object code into executable programs

Our first gcc program: named output “-o” sets name of output Output name is different Output has execute permissions

gcc flags: debug and optimization “gcc –g”: debug symbols – Debug symbols place information in the object files so that debuggers (gdb) can: set breakpoints provide context information when there is a crash “gcc –O2”: optimization – Add optimizations … never fails “gcc –O3”: provide more optimizations – Add optimizations … sometimes fails “gcc –O3 –g” – Debugging symbols slow down execution … and sometimes compiler won’t do it anyways…

Large code development Source code file1.C Source code file1.C Compiler Object code file1.o Object code file1.o Linker Executable code Source code file2.C Source code file2.C Compiler Object code file2.o Object code file2.o Source code file3.C Source code file3.C Compiler Object code file3.o Object code file3.o Why could this be a good idea?

Multi-file development: example cat is a Unix command that prints the contents of a file $? is a shell construct that has the return value of the last executed program

Multi-file development: example

Linker order matters for some linkers (not Macs). Some linkers need the.o with “main” first and then extract the symbols they need as they go. Other linkers make multiple passes. Linker order matters for some linkers (not Macs). Some linkers need the.o with “main” first and then extract the symbols they need as they go. Other linkers make multiple passes.

Libraries Library: collection of “implementations” (functions!) with a well defined interface Interface comes through “header” files. In C, header files contain functions and variables. – Accessed through “#include ”

Includes and Libraries gcc support for libraries – “ -I ”: path to headers for library when you say “#include, then it looks for file.h in the directories -I points at – “-L”: path to library location – “-lname”: link in library libname

Making a static library Note the ‘#’ is the comment character (should have called this libmultiplier.a)

Typical library installations Convention – Header files are placed in “include” directory – Library files are placed in “lib” directory Many standard libraries are installed in /usr – /usr/include – /usr/lib Compilers automatically look in /usr/include and /usr/lib (and other places)

Installing the library (fixing my mistake) “mv”: unix command for renaming a file

Example: compiling with a library gcc support for libraries – “ -I ”: path to headers for library – “-L”: path to library location – “-lname”: link in library libname

Makefiles There is a Unix command called “make” make takes an input file called a “Makefile” A Makefile allows you to specify rules – “if timestamp of A, B, or C is newer than D, then carry out this action” (to make a new version of D) make’s functionality is broader than just compiling things, but it is mostly used for computation Basic idea: all details for compilation are captured in a configuration file … you just invoke “make” from a shell

Makefile syntax Makefiles are set up as a series of rules Rules have the format: target: dependencies [tab] system command

Makefile example: multiplier lib

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

 Due April 14th

Project 1C

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

Unix command: tar Anyone know what tar stands for? tar = tape archiver IBM tape library

Unix command: tar Problem: you have many files and you want to… – move them to another machine – give a copy to a friend – etc. Tar: take many files and make one file – Originally so one file can be written to tape drive Serves same purpose as “.zip” files.

Unix command: tar tar cvf 330.tar file1 file2 file3 – puts 3 files (file1, file2, file3) into a new file called 330.tar scp ssh ix tar xvf 330.tar ls file1 file2 file

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

Memory Errors Array bounds read Array bounds write

Memory Errors Free memory read / free memory write When does this happen in real-world scenarios? Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.

Memory Errors Freeing unallocated memory When does this happen in real-world scenarios?

Memory Errors Freeing non-heap memory When does this happen in real-world scenarios?

Memory Errors NULL pointer read / write NULL is never a valid location to read from or write to, and accessing them results in a “segmentation fault” – …. remember those memory segments? When does this happen in real-world scenarios?

Memory Errors Uninitialized memory read When does this happen in real-world scenarios?

Memory error in action

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

Project 4A Posted now You will practice debugging & using a debugger – There are 3 programs you need to debug In this case, “debug” means identify the bug – Does not mean fix the bug Can use gdb or lldb May want to run on ix Worksheet due in class next week

Outline Review Project 1C Overview Tar Memory Errors Project 4A Background Info for 4A

ASCII Character Set image source: granneman.com There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention

signed vs unsigned chars signed char (“char”): – valid values: -128 to 127 – size: 1 byte – used to represent characters with ASCII values -128 to -1 are not valid unsigned char: – valid values: 0 to 255 – size: 1 byte – used to represent data

character strings A character “string” is: – an array of type “char” – that is terminated by the NULL character Example: char str[12] = “hello world”; – str[11] = ‘\0’ (the compiler did this automatically) The C library has multiple functions for handling strings

Character strings example

Useful C library string functions strcpy: string copy strncpy: string copy, but just first N characters strlen: length of a string

Useful C library string functions strcpy: string copy strncpy: string copy, but just first N characters strlen: length of a string What happened here?

More useful C library string functions source: cplusplus.com

memcpy I mostly use C++, and I still use memcpy all the time

sscanf like printf, but it parses from a string sscanf(str, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval); on: str=“P6\n \n255\n”; gives: magicNum = “P6”, width = 1000, height = 1000, maxval = 255

if-then-else int val = (X < 2 ? X : 2);  if (X<2) { val = X; } else { val = 2; }