Docsity

C and C++ Basics - Operating Systems - Notes | CMPSCI 377, Study notes of Operating Systems

Toggle sidebar
bg1
CMPSCI 377 Operating Systems Spring 2007
Lecture 3: September 13
Lecturer: Emery Berger Scribes: Alex Schultz, Alden Wheeler
3.1 C and C++ basics
3.1.1 Why C over Java?
C is low level and allows direct access to the computer, which gives the programmer a greater degree of
control over the system. In Java, the programmer is insulated form the computer hardware and operating
system by the Java runtime environment.
3.1.2 Systems programming
C is a better choice for systems programming:
๎˜
Fast and space e๎˜Žcient.
๎˜
Low-Level. Very close to the machine architecture.
๎˜
Direct access to memory.
3.1.3 Code Visibility
What you see is what you get.
๎˜
In C, what you code is what the computer does.
๎˜
In Java, each action you take is backed up by many calls done by the runtime environment, whether
or not you absolutely need them all.
Example: Garbage Collection
๎˜
In C, there are no "read" or "write" barriers - that is, garbage collection is not done by the C language.
๎˜
In Java, every time an object reference is changed, the garbage collector must be alerted to check for
references to the old object to see if that object's space can be reused. This tacks on a performance
hit so that one does not have to manage their own variable pointers and references.
3.1.4 Hardware Devices
All the Java code that actually "talks" to the hardware of your computer is written in C.
3-1
pf3

Partial preview of the text

Download C and C++ Basics - Operating Systems - Notes | CMPSCI 377 and more Study notes Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Spring 2007

Lecture 3: September 13

Lecturer: Emery Berger Scribes: Alex Schultz, Alden Wheeler

3.1 C and C++ basics

3.1.1 Why C over Java?

C is low level and allows direct access to the computer, which gives the programmer a greater degree of control over the system. In Java, the programmer is insulated form the computer hardware and operating system by the Java runtime environment.

3.1.2 Systems programming

C is a better choice for systems programming:

 Fast and space ecient.

 Low-Level. Very close to the machine architecture.

 Direct access to memory.

3.1.3 Code Visibility

What you see is what you get.

 In C, what you code is what the computer does.

 In Java, each action you take is backed up by many calls done by the runtime environment, whether or not you absolutely need them all.

Example: Garbage Collection

 In C, there are no "read" or "write" barriers - that is, garbage collection is not done by the C language.

 In Java, every time an object reference is changed, the garbage collector must be alerted to check for references to the old object to see if that object's space can be reused. This tacks on a performance hit so that one does not have to manage their own variable pointers and references.

3.1.4 Hardware Devices

All the Java code that actually "talks" to the hardware of your computer is written in C.

3-2 Lecture 3: September 13

3.1.5 Why C++ over C?

C++ extends C in a couple of ways.

 Upward compatibility: C code can be compiled with a C++ compiler.

 Classes like the ones used in Java for better structuring of data and algorithms.

 Encapsulation allows public/private members of classes.

 Templates, which are analogous to Java generics.

 Inlining instead of macros for increased eciency by inserting function code directly where it is called (Macros also allowed you to do evil substitutions, like switching while loops to if statements)

3.2 Similarities and Dissimilarities between Java and C++

3.2.1 Conditionals

For the most part, conditionals are the same between Java and C++. However, in C++, any non-zero value will evaluate to true.

3.2.2 Command line arguments

In Java, the main function is passed an array of strings that have the command line arguments. In C/C++, the values passed in are a pointer to an array of characters that hold the command line arguments as well as an integer with the size of the array.

3.2.3 Pointers and References

In Java, you are not allowed to directly look at memory addresses, whereas in C++ you can. A pointer is a variable that holds the memory address of another variable and is declared using. A reference is declared using a & and allows you to get the address of a variable so that a pointer may point to it - this is called dereferencing a variable.

3.2.4 Parameter Passing and Returning

In C, all variables are copied when they are passed, which means that any changes made in the function the variable is passed to will not echo back to the originally passed variable. In order to return your results, you should pass in the value to be worked on by reference, and return the value by reference, which will save it from being destroyed after the function dies.

3.2.5 Stack, Heap, and Explicit Memory Management

Variables either live on the stack, which is intended for temporary variables in a function, or on the heap, which is used for persistant objects throughout a program. In C/C++, the heap is explicitly managed by