24
Software Engineering 3156 28-Nov-01 #23: OS, Language, Design Patterns Phil Gross

Software Engineering 3156 28-Nov-01 #23: OS, Language, Design Patterns Phil Gross

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

Software Engineering 3156

28-Nov-01

#23: OS, Language, Design Patterns

Phil Gross

2

Administrivia

Integration prototype is up JSP and Servlets

– Wednesday, Nov 28th, 6-8pm

Final exam– Very little C++/OS– A bit of C

Research Fair this Friday

3

More on C

Will be on next homework Need to be able to do basic stuff Simple C++ is generally easier than C

– Although C++ is capable of being much more complicated

4

Miscellany on structs

Since not everything happens in main, malloc’ing structs is very common– The sizeof operator works on pretty much any

datatype, be it structs or primitive datatypes– Guaranteed tip: you will come across

PointP myP = (PointP)malloc(sizeof(PointP));

– and be glad that you were attending this class…

5

Miscellany on structs

(*myP).x is messy– C has a shortcut: myP -> x– Always remember that a -> b is equivalent to (*a).b– Precedence of operators

It’s cumbersome to have to say struct Point every time– Use typedefs: define “aliases”typedef struct Point PointT;– or even typedef struct Point *PointP;

6

Function prototypes

As I said, C’s compiler is not all that smart If you have:

int main(void) { int i = foo();}int foo(void) { return 5;}

It will not work

7

Function prototypes (II)

Need to clue in the compiler by putting the following prototype before main:int foo(void);

Looks like a Java interface construct Often, you collect the prototypes into a header

(.h) file, then #include it into your various source (.c) files

8

On various .c files…

A common way to build multi-source-file code in C is to – build a set of .h files that export functionality (i.e. “public”

methods)– #include these .h files– Compile and link the .c files together

As long as you have a prototype, the compiler will not care about the definition

– Like compiling against an Interface– The Linker will, though…

9

Useful libc commands (I)

printf: Output text: like System.out.printlnprintf(“%d\n”, intval);printf(“%d%f\n”, intval, floatval);man printf

scanf: Input text: no BufferedReaderscanf(“%d”, &intval)– note the ampersand

Also, fprintf/fscanf and sprintf/sscanf

10

Useful libc commands (II)

gets(string) gets a line of text and puts it in string Janak says: Should technically use fgets(stdin, string) Phil says: Never use gets(). Or if you do, never

complain about Microsoft bugs again. Why not &string? puts() also exists: more like println, actually Can’t use + to concatenate

11

Streams in C

FILEs (almost always FILE *) Open with fopen(), close with fclose() Have fread() and fwrite() Plus fprintf() and fscanf() And ftell()/fseek() for random access Are buffered Have pushback

12

Text to int

atoi(string)– Roughly equivalent to Integer.parseInt, although not

nearly as smart– No exceptions

Can also use sscanf()– Slower, but much more programmable

13

STL Again

Really quite cool Often can do away with most explicit loops Which is a good thing

14

The Idea

How to iterate through an array Method 1: by index Method 2: using pointers See stl_1.cc Next step is obviously templates See stl_2.cc

15

What About Vectors?

Need something “pointer-like” for vectors Has to support *, ++, ==, != Plus some way of getting begin and end values All Standard Library containers (including

vectors) support this! “Pointer-Like-Thingies” are called Iterators Defined inside the container class

16

A Note On Scope

Scope = where a variable is visible C++ allows you to explicitly specify scope

using the :: operator foo::bar

– bar is defined inside class or namespace foo ::baz

– baz is global See scope.cc

17

What Do Iterators Look like?

vector<string> svec;

vector<string>::iterator iter = svec.begin();

What is the type of iter?– Answer: “the iterator type defined inside class

vector<string>”

All container types have functions begin() and end() that return iterators

18

Sooo….

Suppose we templatize our find() function on both elemType and iterType

Now it works on arrays And all containers in the standard library See stl_3.cc

19

What are These Function Objects?

find_if() takes a predicate function Returns values for which it is true How do we do that, exactly? Naïve filter implementation: stl_4.cc Function pointer implementation: stl_5.cc Function objects: zero overhead function-

pointer-like thingies supplied with STL

20

Such As?

Arithmetic– plus<t>, minus<t>, negate<t>, multiplies<t>,

divides<t>, modulus<t>

Relational– less<t>, less_equal<t>, greater<t>,

greater_equal<t>, equal_to<t>, not_equal_to<t>

Logical– logical_and<t>, logical_or<t>, logical_not<t>

21

Example

Create a new sequence that is the sum of fib and pell

transform(fib.begin(), fib.end(),

pell.begin(), target.begin(),

plus<int>() );

22

Seems Kind of Limited

Function adapters add flexibility Bind1st and bind2nd fix the values of the first

or second parameters of a binary function find_if(iter, vec.end(), bind2nd(less<int>, 10))

– Finds first item that is less than 10

You can make your own function objects

23

Maps and Sets

map<string, int> words;

words[“Vermeer”] = 7;

if (words[“Rembrandt”] == 3) {…

set<string> modern;

if (modern.count(“Mondriaan”)) {…

24

What If Our Target is Initially Empty?

Iterator inserters Act like output iterators, but actually call

insertion methods back_inserter(C), front_inserter(C),

inserter(C,i)unique_copy(ivec.begin(), ivec.end(),

back_inserter(result_vec));