Operating Systems
         CMPSCI 377
            C/C++
                   Emery Berger
University of Massachusetts Amhers...
Why C?




  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   2
Why C?
    Low-level


        Direct access to memory
    

        WYSIWYG (more or less)
    

        Effectively n...
OK, Why C++?




  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   4
OK, Why C++?
    C++: extends C


        Upwardly-compatible
    

    Adds significant software engineering


    ben...
Outline, part I
    Basics – compiling & running


    Intrinsic types, conditionals, etc.


    Pointers + Reference va...
Outline, part II
    Functions


        Parameter passing
    

    Structs & classes


    Overloading & inheritance
...
Basics
    Main & compilation





     UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   8
Intrinsic Types
    Essentially identical





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science...
Conditionals
    Mostly the same


        C/C++: nonzero int same as true
    




        UNIVERSITY OF MASSACHUSETTS ...
File I/O
    Simple stream-based I/O


                                               print foo
        cout << “foo”
   ...
Command-line Arguments
    Again, similar to Java





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer...
Key Differences
    Differences between C/C++ and Java


        Assignment
    

        Pointers
    

        Parame...
Assignment
    Java assignment: makes reference


    C++ assignment: makes copy





      UNIVERSITY OF MASSACHUSETTS ...
Pointers & Friends

    Pointers are like jumps, leading wildly from


    one part of the data structure to another.
   ...
Pointers & Friends
    Concept not in Java: address manipulation





      UNIVERSITY OF MASSACHUSETTS AMHERST • Departm...
Functions & Parameter Passing
    C/C++ – all parameters copied by default





      UNIVERSITY OF MASSACHUSETTS AMHERST...
Parameter Passing
    To change input, pass pointer


    or call by reference




      UNIVERSITY OF MASSACHUSETTS AMHE...
Pass by Reference
    Syntactic sugar:


    foo (int &i) = pass by reference
        Secretly does pointer stuff for you...
Stack & Heap




   UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   20
Stack & Heap
    In C/C++ as in Java, objects can live on


    stack or on heap
        Stack = region of memory for tem...
The Stack
    Stack data: new every time





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science ...
Big Stack Mistake
    Never return pointers to the stack!





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of ...
The Heap
    Allocate persistent data on heap with new





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Com...
Explicit Memory Management
    Java heap – garbage collected


    C/C++ – explicit memory management


        You must...
Classes & Objects
    No “top” object (as in Java Object)


        Also: C++ has no interfaces but has multiple
    

 ...
Struct Member Access
    struct = class with everything public


        Use these sparingly
    




        UNIVERSITY...
Class Declaration
    Pretty similar





      UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science   28
Arrays
    Numerous differences


        Arrays do not have to be allocated with new
    

        Array bounds not che...
Other Features
    Operator overloading


        New meanings to existing operators
    

              int operator+(M...
Upcoming SlideShare
Loading in...5
×

Operating Systems - Intro to C++

1,679

Published on

From the Operating Systems course (CMPSCI 377) at UMass Amherst, Fall 2007.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,679
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1,094
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Operating Systems - Intro to C++

  1. 1. Operating Systems CMPSCI 377 C/C++ Emery Berger University of Massachusetts Amherst UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
  2. 2. Why C? UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2
  3. 3. Why C? Low-level  Direct access to memory  WYSIWYG (more or less)  Effectively no runtime system  No garbage collector  No other threads  No “read” or “write barriers”  Efficient  Space & time  C: effectively portable assembly code  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3
  4. 4. OK, Why C++? UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4
  5. 5. OK, Why C++? C++: extends C  Upwardly-compatible  Adds significant software engineering  benefits Classes  Encapsulation (private)  Templates (“generics”)  Other modularity advantages  Inlining instead of macros  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5
  6. 6. Outline, part I Basics – compiling & running  Intrinsic types, conditionals, etc.  Pointers + Reference variables  Assignment  Objects  &, *, ->  Stack vs. heap  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
  7. 7. Outline, part II Functions  Parameter passing  Structs & classes  Overloading & inheritance  Stack vs. heap  I/O, command-line  STL  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7
  8. 8. Basics Main & compilation  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8
  9. 9. Intrinsic Types Essentially identical  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9
  10. 10. Conditionals Mostly the same  C/C++: nonzero int same as true  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10
  11. 11. File I/O Simple stream-based I/O  print foo cout << “foo”  read x from the console cin >> x  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11
  12. 12. Command-line Arguments Again, similar to Java  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12
  13. 13. Key Differences Differences between C/C++ and Java  Assignment  Pointers  Parameter passing  Heap & Stack  Arrays  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13
  14. 14. Assignment Java assignment: makes reference  C++ assignment: makes copy  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14
  15. 15. Pointers & Friends Pointers are like jumps, leading wildly from  one part of the data structure to another. Their introduction into high-level languages has been a step backwards from which we may never recover. C.A.R. Hoare UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15
  16. 16. Pointers & Friends Concept not in Java: address manipulation  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16
  17. 17. Functions & Parameter Passing C/C++ – all parameters copied by default  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17
  18. 18. Parameter Passing To change input, pass pointer  or call by reference UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18
  19. 19. Pass by Reference Syntactic sugar:  foo (int &i) = pass by reference Secretly does pointer stuff for you  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19
  20. 20. Stack & Heap UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20
  21. 21. Stack & Heap In C/C++ as in Java, objects can live on  stack or on heap Stack = region of memory for temporaries  Stack pointer pushed on function entry  Popped on function exit  Heap = distinct region of memory for  persistent objects C/C++ – explicitly managed  Pointers introduce problems!  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21
  22. 22. The Stack Stack data: new every time  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22
  23. 23. Big Stack Mistake Never return pointers to the stack!  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23
  24. 24. The Heap Allocate persistent data on heap with new  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24
  25. 25. Explicit Memory Management Java heap – garbage collected  C/C++ – explicit memory management  You must delete items (or memory leak)  Delete them too soon (still in use) – crash  “Dangling pointer” error  Delete something twice – crash  “Double-free” error  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 25
  26. 26. Classes & Objects No “top” object (as in Java Object)  Also: C++ has no interfaces but has multiple  inheritance – stay far away Key difference for you – not all methods  dynamically-dispatched Methods associated with declared type  rather than dynamic type unless labeled virtual UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 26
  27. 27. Struct Member Access struct = class with everything public  Use these sparingly  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 27
  28. 28. Class Declaration Pretty similar  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 28
  29. 29. Arrays Numerous differences  Arrays do not have to be allocated with new  Array bounds not checked  Item[0] = pointer to start of array  Arrays just syntactic sugar  for pointer arithmetic! (scary! avoid!) v = 12; *(Item + v) = 1;  Same as Item[12] = 1;  Note: sizeof(x) = number of bytes to hold x  Multi-dimensional arrays (matrices)  just arrays of pointers to arrays  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 29
  30. 30. Other Features Operator overloading  New meanings to existing operators  int operator+(MyType& a, MyType& b);  Controversial, but useful for things like  complex math, matrix operations int& operator()(int x, int y);  Templates  A.k.a. generics in Java  template <class X> void foo (X arg);  UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 30
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×