csc 253 lecture 2. some differences between java and c compiled c code is machine specific, whereas...

12
CSC 253 Lecture 2

Upload: rosanna-morrison

Post on 01-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

CSC 253CSC 253

Lecture 2Lecture 2

Page 2: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Some differences between Java and C

Some differences between Java and C

Compiled C code is machine specific, whereas Java compiles for a virt. machine. Virtual machines are much slower than natively

executed code. Compiled code can interact with specifics of

that machine (e.g., devices). Java does “automatic” memory

management. In C, you must free memory explicitly.

You could forget to do it … this causes a memory leak. However, you can get by with less memory. And, you don’t have to worry about garbage collection

causing you to miss real-time deadlines.

Compiled C code is machine specific, whereas Java compiles for a virt. machine. Virtual machines are much slower than natively

executed code. Compiled code can interact with specifics of

that machine (e.g., devices). Java does “automatic” memory

management. In C, you must free memory explicitly.

You could forget to do it … this causes a memory leak. However, you can get by with less memory. And, you don’t have to worry about garbage collection

causing you to miss real-time deadlines.

Page 3: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

C vs. JavaC vs. Java

Why are OSs written in C? The very bottom layer of sw. has to be native

machine code. Two steps in the Java compile-run sequence

are “skipped” with C. What two steps?

Interpretation—JVM xlates byte code to machine code. What’s the advantage of skipping them?

Faster What’s “bad” about skipping them?

Machine dependence. No place for run-time bounds-checking.

Why are OSs written in C? The very bottom layer of sw. has to be native

machine code. Two steps in the Java compile-run sequence

are “skipped” with C. What two steps?

Interpretation—JVM xlates byte code to machine code. What’s the advantage of skipping them?

Faster What’s “bad” about skipping them?

Machine dependence. No place for run-time bounds-checking.

Page 4: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

PointersPointers

Advantages? You can perform arithmetic on pointers

… this may save a few instructions. You can get a pointer to anything in C.

Disadvantages? “Dangling pointers”—pointers that don’t

point to allocated memory anymore. Forgetting to dereference a pointer.

Advantages? You can perform arithmetic on pointers

… this may save a few instructions. You can get a pointer to anything in C.

Disadvantages? “Dangling pointers”—pointers that don’t

point to allocated memory anymore. Forgetting to dereference a pointer.

Page 5: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Summary: Advantages of C

Summary: Advantages of C

Efficiency Close to the machine Uses much less memory You have better control of your

program (talking directly to machine).

Efficiency Close to the machine Uses much less memory You have better control of your

program (talking directly to machine).

Page 6: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Summary: Advantages of Java

Summary: Advantages of Java

Portability Safety Less error prone

Portability Safety Less error prone

Page 7: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Variables used by > 1 function

Variables used by > 1 function

Should they be … Global? …

How can such a value be passed into a function?

How can such a value be returned from the function?

Should they be … Global? …

How can such a value be passed into a function?

How can such a value be returned from the function?

Page 8: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Why not use lots of globals?

Why not use lots of globals?

You can prevent accesses to variables from places they’re not supposed to be accessed from.

Name-space pollution. Local variables are deallocated

upon procedure returns, whereas global variables aren’t. (Uses more memory.)

You can prevent accesses to variables from places they’re not supposed to be accessed from.

Name-space pollution. Local variables are deallocated

upon procedure returns, whereas global variables aren’t. (Uses more memory.)

Page 9: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

How can you avoid globals?

How can you avoid globals?

Pass pointers to structures. You can return it from a function.

You can pass in & return pointers, and in the function, modify what’s pointed to.

Pass pointers to structures. You can return it from a function.

You can pass in & return pointers, and in the function, modify what’s pointed to.

Page 10: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Why should you not do arithmetic on characters?

Why should you not do arithmetic on characters?

Page 11: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Let’s write a program to …Let’s write a program to …

determine the value of INT_MAX; that is, show an integer i that

prints as positive, whereas i+1 prints as negative.

determine the value of INT_MAX; that is, show an integer i that

prints as positive, whereas i+1 prints as negative.

Page 12: CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines

Let’s write a program toLet’s write a program to

look at the bits in INT_MAX and see how it’s represented in our computer.

look at the bits in INT_MAX and see how it’s represented in our computer.