Slide 1

Slide 1 text

C Programming on Linux A brief introduction to C and our Linux environment 1

Slide 2

Slide 2 text

Agenda • Housekeeping • Why C? • Pointers (and Strings) • Beej’s Guide • Working on A3 2

Slide 3

Slide 3 text

Housekeeping • A1 grades are out • A2 demos this week and next • A3 to be released very soon • Today we’re prepping for A3 3

Slide 4

Slide 4 text

Why C? • Still systems language of choice! • Low-level access • Portability, speed, size • Terse syntax over assembly • No runtime required • Long, proud history in OS development 4

Slide 5

Slide 5 text

Pointers • Your process has an address space • You access memory by address • A pointer is simply an address 5

Slide 6

Slide 6 text

Pointers char *ptr = an address ... ptr vs. *ptr An address is represented by an integer type 6

Slide 7

Slide 7 text

Pointers • Point to data • Point to code • Point to nothing (oops!) 7

Slide 8

Slide 8 text

Pointers Fine, but where do these addresses come from? void* malloc (size_t size); ptr = &var; 8

Slide 9

Slide 9 text

Strings • In C, a string is an array of chars • char  s1[128]; • char  *s2  =  (char*)malloc(128); • s1, s2 are both pointers!   9

Slide 10

Slide 10 text

Strings • There’s no s1.length() • Instead: NULL byte at the end • strlen(s1) counts until it finds a “\0” • Don’t use string functions on binary data! 10

Slide 11

Slide 11 text

Beej’s Guide • Google it • We’re doing network programming • Lots of examples in C! http://beej.us/guide/bgnet/ 11

Slide 12

Slide 12 text

Working on A3 • Need to compile, debug, test on linux • Specifically: our ugrad linux machines $  ssh  [email protected] Last  login:  Fri  Mar  15  07:58:08  2013  from  sonora.cs.ubc.ca Have  a  lot  of  fun... deas:~>  cat  /etc/*-­‐release NAME=openSUSE VERSION  =  12.1  (Asparagus) VERSION_ID="12.1" PRETTY_NAME="openSUSE  12.1  (Asparagus)  (x86_64)" ID=opensuse openSUSE  12.1  (x86_64) VERSION  =  12.1 CODENAME  =  Asparagus 12

Slide 13

Slide 13 text

Working on A3 • Linux: mount remote.ugrad.cs.ubc.ca • OSX: Fuse for OSX + Macfusion or ssh • Windows: Putty + Xmanager or Xming • Don’t use Windows ports (cygwin)! Suggestions 13

Slide 14

Slide 14 text

Working on A • Platform matters! • Compilers produce different code • Timing issues, library versions = headaches • Build and test your code on ugrad servers! • Consider DDD + GDB for debugging 14

Slide 15

Slide 15 text

Working on A3 • More about A3 • Sample dev environment (OSX) • Network programming in C • Debugging C programs 15