Download presentation
Presentation is loading. Please wait.
Published byRosanna Morrison Modified over 9 years ago
1
CSC 253 Lecture 2
2
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.
3
C 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.
4
Pointers 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.
5
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).
6
Summary: Advantages of Java Portability Safety Less error prone Portability Safety Less error prone
7
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?
8
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.)
9
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.
10
Why should you not do arithmetic on characters?
11
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.
12
Let’s write a program to look at the bits in INT_MAX and see how it’s represented in our computer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.