Outline for Recitations, August 27, 1997 Today: o Input & output in C++ o Pointers, arrays, strings, basics of memory alloc o unix overview [save 2 min for this even if don't get through above] Basically at end should have covered material in handout cxxbasics_one.ps, plus talked some about the lab and a bit about unix. ============================================================================= Input/Output in C++ (should be review) #include /* standard i/o header file in C++ */ The standard iostreams are called cin and cout. To print a value out, we write: cout << "The number of instructors is "; cout << instructors; cout << ".\n"; or cout << "The number of instructors is " << instructors << "." << endl; To read a value, we write: cout << "Please enter two integer values"; cin >> val1 >> val2; // note that we do NOT write &val1, &val2 like with scanf in C. A third I/O stream, cerr, is used to print error messages. cerr << "programmer must have screwed up"; To get a single character: char ch; cin.get(ch); // what's going on here? shouldn't we pass &ch? // this function is using call-by-reference. // we'll see more about this later. Interesting to compare with C: To use library input/output routines in a C program, we write: #include In C, to print values to stdout (standard output stream), we write: printf("name of team %s \n",teamname); /* prints out a string */ printf("number of players %d \n",players); /* prints out an integer */ In C, to read values from stdin (standard input stream), we write: printf("enter number of students"); scanf("%d",&students); /* pass pointer to int */ printf("enter team name: "); scanf("%s",teamname); /* pass string (char *) */ Note: standard C I/O functions will work in C++, as long as we include stdio.h and synchronize with ios::sync_with_stdio() Arrays and Pointers int a[5] = {0,2,4,6,8}; a is address of first element of array. (Draw picture). a: 1024 a: ---------+ \|/ ----------------------------- 1024 | 0 | 2 | 4 | 6 | 8 | ----------------------------- int *p; Means that the value of p is an address, and in particular, the address of an integer. p = a+2; --> draw new picture. *p is value pointed to by p. *a is same as a[0] *(a+3) is same as a[3] play around with this. When you pass into a procedure, arrays and pointers are the same: both treated as addresses of a character. Strings A String is a NULL-terminated array of characters. e.g., char str[6]; strcpy(str, "hello"); ------------------------------------ str: | 'h'| 'e' | 'l' | 'l' | 'o' | '\0' | ------------------------------------ if we now set str[0] = 'y'; we get "yello" void strcpy(char *to, char *from); // copies from into to, including '\0' // assumes enough storage in "to". void strcpy(char *to, char *from) { while ((*to = *from) != '\0') { ++from; ++to; } } DOES THIS COPY THE '\0'? yes. Why? Reason: copy first, THEN compare. Try an example of running it on something. more cryptically: void strcpy(char *to, char *from) { while(*to++ = *from++); } Memory allocation char *ptr; // a character pointer, but no space to put anything. strcpy(ptr, str); // error. God knows where ptr is pointing. ptr = new char[10]; // space for 10 chars. (currently filled with garbage) strcpy(ptr, str); // the first 6 chars are hello0. Then garbage. delete []ptr; // all done. Free it up. Here is some code from Lab 1 (slightly modified). #include // // return a string in which each period in "string" is replaced by "num" // exclamation marks. // char* shout(char* string, int num) { int len = strlen(string), num_extra = 0; // How much extra storage do we need? for(int i = 0; i < len; i++) if(string[i] == '.') num_extra += num-1; char* shout_string = new char[len+num_extra+1]; // why the +1? for(int i = 0, j = 0; i <= len; i++) { if (string[i] != '.') { // Do the simple case first. shout_string[j++] = string[i]; } else { // convert period to num exclamations for(int k=0; k < num; ++k) { shout_string[j++] = '!'; } } } return shout_string; } Unix Overview: -------------- (There is a Unix primer that can be reached from our course web page.) Some basics: use ctrl-C to halt a program. ctrl-Z to suspend. "foo &" will run the program "foo" in the background. If you are running "foo" and want to send it into the background, can type "ctrl-Z" and then "bg". To get something back to the foreground, type "fg". E.g., % foo <-- let's run foo ...... <-- foo is running. ctrl-Z <-- let's suspend foo Suspended <-- this is unix telling you that foo got suspended % fg <-- bring foo back foo <-- this is unix telling you foo is back ...... ctrl-C <-- let's kill foo % can hook up cin and cout to files like this: % foo < input1 <- cin connected to input1 % foo > output1 <- cout connected to output1 (note: cerr will still print to the screen. Useful if you want to ask the user a question.) % foo < input1 > output1 <- reads from input1, prints to output1. % cat input1 | foo > output1 <- another way to do it. xemacs: a good editor for C++ programs. ^h-t runs a tutorial, ^h-^h lists help availables, c++ mode offers lots of help ^h-m: tells you what the mode offers ^h-i: info mode (very useful collection of online manuals) man: manual pages. gmake