1 / 14

Introduction to Programming in C / C++ / ROOT

Introduction to Programming in C / C++ / ROOT. Christopher Crawford 2008-10-01. Computer Architecture. CPU (32/64 bit words) Instructions Read / Write / Arithmetic Branching / Subroutines Registers Program counter Stack counter (subroutines) Pre-emptive multitasking

Download Presentation

Introduction to Programming in C / C++ / ROOT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming in C / C++ / ROOT Christopher Crawford 2008-10-01

  2. Computer Architecture • CPU (32/64 bit words) • Instructions • Read / Write / Arithmetic • Branching / Subroutines • Registers • Program counter • Stack counter (subroutines) • Pre-emptive multitasking • Memory – bytes (8 bit bytes) • Address space (address, data): • data (variables) • text (CPU instructions) • I/O (not memory at all) • Virtual memory (paging) • Input / Output (N-bit buses) • busses • PCI- expansion slots, Ethernet • IDE, SATA - hard drive (HD) • USB – keyboard, mouse • LPC - serial, parallel, floppy • IRQ – interrupt requests • DMA – direct memory access Cache Registers ALU

  3. Operating System • BIOS – first instructions executed, loads and runs OS kernel • Kernel – controls CPU • Process control • Scheduler (multitasking) • Environment for processes • Threads (separate execution lines in a single program • Memory management • Paging (swap space) • Segmentation violations! • I/O devices • File system • All external data mounted into common directory tree • Devices are treated as files • /dev/ttyS1 • /dev/stdin, /dev/stdout ) • Modules • Device drivers, accesses I/O • Shell • User interface to the kernel • Interpreted ‘language’ • Shell scripts • Eg.: bash, tcsh, tcl, perl • Utilities • programs which further interact with kernel (process, mem, i/o) • Eg.: ls, cd, cp, mv, ps, mount, etc • Compiler • Compiler chain • Preprocessor (#include) • Compiler (.c  .s) • Assembler (.s  .o) • Linker (.o  .so, a.out) • Loader (.so, a.out  memory) • Build tool chain • Configure (autoconf) • Make (automake) • Debugging (gdb, gprof) • Libraries: *.so *.a *.h • Std C library (glibc)

  4. File System • All I/O is organized into one tree • Path: parent/child/grandchild/… • Mounts: (instead of a: b: c: drives) • Single disk at base of file system • Other disks ‘mounted’ (grafted) onto branches of file system • Drivers, raw I/O: /dev • Kernel I/O: /proc, /sys • Special directories • Root (base) / • Current (cwd) . • Parent .. • Home ($HOME) ~ • Directory structure • Std names for common directoriesbin, etc, lib, include, share, src • Repeating hierarchy of structure/, /usr, /usr/local, /usr/X11, /opt, ... / - root directory etc - configuration bin - executable (binaries) sbin - secure code lib - libraries var - databases, spools, .. tmp - temporary files boot/ - kernel image mnt/ usb,floppy - mounted directories dev/ - I/O devices zero,null,ttyS0,parport0 std{in,out,err} -> /proc/self/fd/{0,1,2} sca,sca0,sca1, scb,scb0, .., cdrom -> scd0 proc/ - CPU processes cpuinfo,meminfo, - diagnostics 16117/cwd,environ,cmdline fd/{0,1,2} -> /dev/pts/{0,1,2} sys/ - system info usr/bin,lib,share… - distribution software local/bin,lib,… - custom software X11/bin,lib,… - GUI software opt/ - commercial software root/ - superuser directory home/ - user directories chris2/ etc,bin,lib data/ - shared data

  5. bash – Bourne-again shell • Program execution • ENVIRONMENT variables • Input / Output redirection • Utilities • grep • find • sed

  6. Memory structure Address Space • Size in bytes (not words) • 4 GB (32 bit pointers) • Memory chart: • type (C/C++) • symbol (assembly) • address (hardware) • data (hardware) • Program parts: • Text (code) • Data (globals) • Stack (auto,temp) • Heap (dynamic) • Type modifiers: • auto, static, extern • const, volatile • [un]signed Data Types • Discrete • bool 2 1 bit • char 256 8 bits 1 byte • short 65536 16 bits 2 bytes • integer 4.295G 32 bits 4 bytes 1 word • long long 18.447E 64 bits 8 bytes 2 words • Continuous • float 7e-7 32 bits 4 bytes 1 word • double 2e-16 64 bits 8 bytes 2 words • Compound • void none (no type) • pointers 32/64 CPU dependent • references 0 alias only • arrays N * base type • structures Σ base types

  7. Sample C program #include <stdio.h>/* library function prototypes */ #include <string.h> int n = 1; /* variable declarations */ floatx = 2.3; int add( floatx, floaty ); /* function prototype */ int main( int argc, char** argv ) /* main program */ { int y = add( n, x ); /* automatic variable declaration */ y = y + 1; /* statement with assignment operator */ printf("y = %d\n", y); /* formatted print statement */ charname[80]; /* character string (array) declaration */ printf( "who are you?\n" ); fgets( name, 80, stdin ); if (!strcmp( name, "joe\n" )) { /* conditional branching */ int i; for ( i=0; i<5; ++i ) { /* loop */ printf( "go away!\n" ); } } else { printf( "hello, %s", name ); } return 0; /* return value of function */ } int add( floatx, floaty ) /* subroutine function */ { return x + y; }

  8. Structure of a C program • Essentially a collection of declarations and definitionsof variables and functions • Execution starts at • int main ( int argc, char** argv) { • Everything must be defined before it is used • Strongly typed • Compiler only scans file once • Types are defined in the same way they are used • int fn ( float var, …) • Function definitions are lists of variable declarations and statements executed linearly • Statements consist of a) Expressions, b) Blocks, c) Flow Control • Expressions • variables and constants tied together with operators (functions) • Blocks – group of statements { statement; statement; ... } • Flow control Statements • Conditionals if (cond) statementelsestatement switch (key) { case label: statement; break; default: statement; } • Loops while (cond) statement dostatementwhile (cond); for (init; cond; incr) statement break; continue; (inside loops) goto label; label: statement • Exceptions trystatement catch (err) statement throwerr;

  9. C/C++ punctuation /* */ C-style comments // C++-style comments { } statement block, array initializer data [ ] array index, declaration ( ) function arguments, expression ordering, initializer < > templates, #include <stdlib.h> "string" character string 'c' single character , separates function arguments ; end of statements and declarations : statement label # preprocessor directives A-Za-z_0-9 valid characters in a name, first letter cannot be 0-9

  10. C/C++ Operators – precedence & associativity :: (unary <-, binary ->) scope () [] -> . -> arguments, element, index, member ++ -- + - ! ~ (unary) <- inc/decrement, pos., neg., not, complement (type) * & sizeof typecast, dereference, address, size .* ->* -> function pointer * / % -> multiply, divide, modulo + - -> add, subtract << >> -> bit shift left/right; (stream insert/extract) < <= > >= -> comparison == != -> test equality & -> bitwise and ^ -> " xor | -> " or && -> logical and || -> " or ?: <- if-then-else = += -= *= /= %= <- assignment &= ^= |= <<= >>= , -> sequence

  11. IEEE Floating Point Numbers • Integers

  12. Standard C library – glibc • <complex.h> – complex number arithmetic and functions • <math.h> – trigonometric and exponential functions • <signal.h> – signal handling • <stdarg.h> – functions with indefinite # of arguments • <stdio.h> – standard input / output • <stdlib.h> – memory allocation, process control, conversions, random numbers • <string.h> – string comparison, memory copying • <time.h> – time and date manipulation and formatting • <assert.h> – debugging, report assertions which are not true • <ctype.h> – character types • <errno.h> – error conditions • <fenv.h> – floating point environment • <float.h> – floating point parameters • <inttypes.h> – macros for printf • <iso646.h> – words for logical operators • <limits.h> – limits of each number type • <locale.h> – international messages • <setjmp.h> – longjump • <stdbool.h> – boolean type • <stddef.h> – NULL pointer • <stdint.h> – portable integer types • <tgmath.h> – float math functions • <wchar.h> – 2-byte characters (unicode) • <wctype.h> – 2-byte characters (unicode)

  13. Argument passing between functions • Arguments are passed on the execution stack • First temporary variables of the function • Three different argument-passing styles, based on compound types: • A) pass by value int fn ( int a ) fn ( x ) • Value of a copied, used to create new variable ‘a’ on the stack • Original variable ‘x’ remains untouched • B) pass by reference int fn ( int & a ) fn ( x ) • New symbol ‘a’ labeling old variable, old value is rewritten • Arguments can be used to return multiple values from function • Only variable names can be used as function arguments (not just a value) • This is the FORTRAN method of passing arguments • C) pass by pointer int fn ( int * a ) fn ( &x ) • New pointer variable ‘a’ created on the stack, points to old value of ‘x’ • Clunky way of implementing (B) • Arrays are always passed by address only: • Array type int fn ( int v[3] ) fn ( v ) • (pointer) int fn ( int * v ) fn ( v ) • Double array int fn ( int w[][3] ) fn ( w ) • (pointer) int fn ( int (*w)[3] ) fn ( w )

More Related