1 / 32

COMS W3156: Software Engineering, Fall 2001

COMS W3156: Software Engineering, Fall 2001. Lecture #21: C, C++ Janak J Parekh janak@cs.columbia.edu. Administrativia. “Where’s the !($#%@)! prototype???” Wednesday! Moved code review deadline Interested in doing research?

sveta
Download Presentation

COMS W3156: Software Engineering, Fall 2001

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMS W3156:Software Engineering, Fall 2001 Lecture #21: C, C++ Janak J Parekh janak@cs.columbia.edu

  2. Administrativia • “Where’s the !($#%@)! prototype???” • Wednesday! • Moved code review deadline • Interested in doing research? • Come see me, or consider the research fair happening next Friday, the 30th, 10am-4pm • http://acm.cs.columbia.edu/research • For the fair, you need to sign up, but free food!

  3. Project update • Deformalize (demodalize) chat? • Needed for logging • Formal seems too cumbersome • Software Engineering problem: keep reqs or change them? • ChangeOther for bots • How to communicate to server? • Introduce RequestActorChange, or in MapDelta?

  4. Project update (II) • Death sequence? • Attacked message, with death boolean set to true • Kick the user from the game • Delete the object from the map • Remove the player from LDAP: HP no longer make sense • WrappedOutputStream: need to use for final implementation • WebServer: to serve up Images correctly, need Content-Length • I’ve put up my WebServer

  5. Next class • Grr, Phil has one more class than we do • You can go attend his class tomorrow if you like: heavy C++ treatment • My next class begins operating systems and “design patterns” • I’ll probably go over a bit more C++ • But also recitation next week

  6. Today’s class • Finish C • Intro to C++

  7. Pointer examples int x = 7; int *ip = &x; printf(“%d\n”, ip); // What does this print? printf(“%d\n”, *ip); *ip = 9; printf(“%d\n”, *ip); printf(“%d\n”, x);

  8. Pointers: miscellany • If ip == 4, and ints are 4 bytes long, what does ip+1 equal? • This is why pointers are “typed” • C does not have real arrays • Basically consecutive blocks of memory pointed to int a[7]; a == &a[0]; ++a == &a[1]; • Nothing, even pointers, are null-initialized

  9. C’s use of pointers • C does not have Strings either • Basically an array of chars (char *) • Basically a pointer to a block of memory • Char pointers are all over the place • C’s loose typing: the meaning of “0” • False • End-of-string character • Null pointer (#define NULL 0)

  10. Using pointers void foo(int *i) { *i = 5; } int main(void) { int i = 0; foo(&i); }

  11. Using pointers (II) • Note that * and & essentially “cancel each other out” • What if we didn’t have anything to point the pointer to, initially? • What if we want to allocate memory in general for a pointer/reference without a “static” variable?

  12. This is bad int *foo(void) { int i = 5; return &i; } int main(void) { int *j = foo(); }

  13. Huh? • I lied: C has one form of garbage collection • Local variables that are allocated get cleaned up • Well, not really cleaned up: just deallocated • Need to manually allocate memory and assign it to a pointer • The malloc() command • Think of it as a poor man’s new()

  14. Much better int *foo(void) { int *i = (int *)malloc(sizeof(int)); *i = 5; return &i; } int main(void) { int *j = foo(); }

  15. Oh dear… • Any memory you malloc(), you must then free() • Unless you want to wait till the program ends • You don’t want to • “Save the whales. Free the mallocs.” • Otherwise, you’ve got an (insidious) memory leak • Again, philosophy is that you do everything

  16. Sidebar: C == evil void foo(char *dst, char *src) { while (*dst++ = *src++); } • Yes, this code actually compiles and runs • What does it do? • * has higher precedence than ++ • Strings terminated by a “\0”, or 0 character, which is false • Wacky, huh?

  17. Passing multiple values • So, in Java, how do you pass multiple values? • Lots of parameters (bad) • Pass a complex Object into a method call, and have it modify the Object • Return a new Object • In C? • No objects! • But, there are structs…

  18. Struct • Basically, a class without methods struct Point { int x; int y; }; /* note the semicolon */ int main(void) { struct Point myPoint; myPoint.x = 5; myPoint.y = 5; }

  19. Now, how to pass this around? • You don’t want to pass it call-by-value • Slow • Can’t return results back all that easy • You want to return it via call-by-reference • Pass a pointer to the struct around

  20. Back to foo() struct Point { int x; int y; }; void foo(struct Point *myP) { (*myP).x = 5; (*myP).y = 5; } int main(void) { struct Point myPoint; foo(&myPoint); }

  21. Miscellany on structs • (*myP).x looks ugly • C has a bizarre shortcut: myP -> x • Yes, a dash followed by a greater than • 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;

  22. More 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…

  23. 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

  24. 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

  25. 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…

  26. C++ • Not strictly a superset of C, but C++ compilers backwards-compatible with C • Certain subtle language differences • C with classes, but a whole lot more • Function, operator overloading • Powerful “template” functionality (Phil – tomorrow) • Exceptions

  27. Quick primer to C++ • g++ is the GNU C++ compiler • Actually a C++ plugin to gcc • Can feed C code to it, will work fine • Can integrate C and C++ code pretty easily • However, ideally, you want to avoid using many C constructs

  28. Hello, world in C++ #include <iostream> using namespace std; int main(void) { cout << “Hello world\n”; } • Note use of namespaces, streams • << operator is overloaded to allow combination/concatenation of strings with streams • Can do cout << “foo” << “bar” << endl;

  29. Class declarations in C++ class Rectangle { private: double width; double height; public: Rectangle(int width, int height); ~Rectangle(); void setWidth(int width); int getArea(); // and so on… }

  30. Implementing methods • Inline in the class declaration • Or, use the class as a prototype and implement elswhere void Rectangle::setWidth(int w) { width = w; }

  31. Constructors and destructors • You have both, not just one • Motivation of destructor: cleanup the object, free any memory, etc. • Don’t use C++’s default denstructor if you new anything • You should delete anything you new • Don’t use malloc/free unless absolutely necessary

  32. C++ miscellany • Operator overloading: nice, but must be careful • Often need to override assignment and copy operators if you have funky data structures (e.g, the “=” operators) • References in function prototypes (finally!) • Templates: “type” data structures • STL: Standard Template Library • You now have “string”s! • Lots of other data structures • Somewhere between C and Java API’s • Very, very fast if used correctly

More Related