no exercise before Wednesday
hw1 due tomorrow
extra office hour today 4–5
why not fsync after every write?
performance

FILE *) flushes libc cache to kernelfileno() to get fd from FILE *readdir vs readdir_rstruct dirent * readdir(DIR *dirp);
simple to use; thread safety issue.
readdir_rint readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
struct dirent {
...
char d_name[256]; /* filename */
};
struct dirent entry;
readdir_r(dirp, &entry, ...); /* BAD: long filename */
name_max = pathconf(dirpath, _PC_NAME_MAX); /* large enough? */
size_t len = offsetof(struct dirent, d_name) + name_max + 1;
struct dirent *entryp = malloc(len); /* man readdir_r */
readdir_r(dirp, entryp, ...); /* BAD: race */
C++ history
overview
namespace & overloading
desgined by Bjarne Stroustrup (wav)
1979 - C with Classes, Cpre
1983 - C++, Cfront
1990 - Annotated C++ Reference Manual, Turbo C++
1993 - Standard Template Library (STL)
1998 - C++98, first C++ standard
2011 - C++11 (C++0x), boost
2014 - C++14

C++ is a general purpose programming language with a bias towards systems programming that
- is a better C
- supports data abstraction
- supports object-oriented programming
- supports generic programming.
main, primitive types, pointersnullptr (vs NULL)new/delete (vs malloc/free)structparametric polymorphism / generics
example: vector of ints, floats, and vectors of floats
vector<int> x;
vector<float> y;
vector<vector<float>> z;
C standard library
containers: bitset, list, queue, set, stack, hashtable, vector, …
strings & regular expressions
I/O streams
multithreading
many more …
C++ is huge and evolving
can easily hurt yourself & go wrong
get your hands dirty and write more code
use tools: compile warnings, cpplint.py, valgrind
think how each C++ feature can be implemented in C
two C++ libraries: libx and liby
foo() and struct Pair
libx.h, defined in libx.ccfoo() and struct Pair
liby.h, defined in liby.cctest.cc includes both libx.h and liby.h$ make CXX=g++
g++ -c -o test.o test.cc
In file included from test.cc:2:
./liby.h:3:8: error: redefinition of 'Pair'
struct Pair {
^
./libx.h:3:8: note: previous definition is here
struct Pair {
^
1 error generated.
C solution: add prefix: “libx_” and “liby_” to structs/functions
C++ solution: enclose structs/functions with namespace
q: what does g++ actually do?
$ nm libx.o
... T __ZN4libx3fooEv
$ nm liby.o
... T __ZN4liby3fooEv
use c++filt
$ nm libx.o | c++filt
... T libx::foo()
$ nm liby.o | c++filt
... T liby::foo()
g++ prepends namespace to function names
void foo(char y) { }
void foo(int x) { }
q1: does this work in C? no - conflict!
q2: how does the C++ compiler implement this?
use nm
... T __Z3fooc
... T __Z3fooi
name mangling: C++ compiler changes function names
g++: void foo(int x) → __Z3fooigcc: void foo(int x) → _fooq: is return type encoded?
q: define two C++ functions that differ only by return type?
q: compile *.cc using different C++ compilers?
void foo(void)foo()a.h and call foo()invoke C function in C++: extern "C"
struct point {
int x, y;
};
point point_add(point a, point b) {
point c = {a.x + b.x, a.y + b.y};
return c;
}
customize operators: +, -, <<, >>, …
improve code readability: “point_add(px, py)” vs “px + py”
be consistent & reasonable: don’t define + as -
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const char *s = "hello";
int x = 42;
printf("%s %d\n", s, x);
return EXIT_SUCCESS;
}
#include <iostream>
#include <cstdlib>
int main(int argc, char **argv) {
std::string s = "hello";
int x = 42;
std::cout << s << " " << x << std::endl;
return EXIT_SUCCESS;
}
in-class exercises
bring laptop or pen & paper