C Programming on Linux

C Programming on Linux

CPSC 317 Tutorial, March 15th 2013.

3c02038387302217d6b670ba6dafaecf?s=128

Kalan MacRow

March 15, 2013
Tweet

Transcript

  1. C Programming on Linux A brief introduction to C and

    our Linux environment 1
  2. Agenda • Housekeeping • Why C? • Pointers (and Strings)

    • Beej’s Guide • Working on A3 2
  3. Housekeeping • A1 grades are out • A2 demos this

    week and next • A3 to be released very soon • Today we’re prepping for A3 3
  4. 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
  5. Pointers • Your process has an address space • You

    access memory by address • A pointer is simply an address 5
  6. Pointers char *ptr = an address ... ptr vs. *ptr

    An address is represented by an integer type 6
  7. Pointers • Point to data • Point to code •

    Point to nothing (oops!) 7
  8. Pointers Fine, but where do these addresses come from? void*

    malloc (size_t size); ptr = &var; 8
  9. Strings • In C, a string is an array of

    chars • char  s1[128]; • char  *s2  =  (char*)malloc(128); • s1, s2 are both pointers!   9
  10. 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
  11. Beej’s Guide • Google it • We’re doing network programming

    • Lots of examples in C! http://beej.us/guide/bgnet/ 11
  12. Working on A3 • Need to compile, debug, test on

    linux • Specifically: our ugrad linux machines $  ssh  kalanwm@remote.ugrad.cs.ubc.ca 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
  13. 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
  14. 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
  15. Working on A3 • More about A3 • Sample dev

    environment (OSX) • Network programming in C • Debugging C programs 15