Professional Documents
Culture Documents
Andre Kessler
Andre Kessler
1 / 30
Outline
Motivation
Class Logistics
Memory Model
Compiling
Wrap-up
Andre Kessler
2 / 30
3 / 30
4 / 30
Motivation
Why C or C++?
Speed
Graph of program speed across language implementations removed due to copyright restrictions.
Source: http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.php.
Andre Kessler
5 / 30
Motivation
Why C or C++?
Power
C: direct access to memory and memory management, expressive but
terse
C++: all the power of C, plus stronger typing, object-oriented and
generic programming, and more
Andre Kessler
6 / 30
Motivation
Why C or C++?
Ubiquity
C: operating systems, drivers, embedded, high-performance computing
C++: large software projects everywhere
Examples: Linux kernel, Python, PHP, Perl, C#, Google search
engine/Chrome/MapReduce/etc, Firefox, MySQL, Microsoft
Windows/Office, Adobe Photoshop/Acrobat/InDesign/etc, lots of
financial/trading software, Starcraft, WoW, EA games, Doom engine,
and much, much more
Andre Kessler
7 / 30
Motivation
Eective Programming
Andre Kessler
8 / 30
Motivation
Eective Programming
Andre Kessler
9/ 30
Class Logistics
Course Syllabus
Day
1
2
3
4
5
6
7
8
9
10
Andre Kessler
Topic
Introduction to C: memory and the compiler
Subtleties of C: memory, oating point
Guest lectures: Assembly and Secure C
Transition from C to C++
Object-oriented programming in C++
Design patterns and anti-patterns
Generic programming: templates and more
Projects: putting it all together
Projects: continued
Grab-bag: coding interviews, large projects
10 / 30
Class Logistics
Grading
6 units U credit, graded Pass/Fail
Coding assignments
Three assignments worth 20%, nal worth 40%.
Automatic instantaneous feedback
Code reviews
Two reviews of code by your peers
More details later
To Pass
at least 50% of available coding assignment points
must submit both code reviews
Andre Kessler
11 / 30
Class Logistics
Textbooks
None required.
However, the following books are on reserve at the library and may be
useful as references. Highly recommended if you end up doing more
C/C++ coding after this course.
Recommended
The C Programming Language by B. Kernighan and D. Ritchie (K&R)
The C++ Programming Language, 4th ed. by Bjarne Stroustrop
Eective C++, More Eective C++, and Eective STL by Scott Meyers
Andre Kessler
12 / 30
Class Logistics
To run: ./nothing
Andre Kessler
13/ 30
Class Logistics
Hello, world!
Andre Kessler
14 / 30
Class Logistics
Hello, world!
Andre Kessler
15 / 30
Class Logistics
Hello, world!
Andre Kessler
16 / 30
Class Logistics
Hello, world!
hello.c: takes no arguments, prints Hello, world!, returns 0
#include <stdio.h>
int main(void) {
printf( "Hello, world!\n" );
return 0;
}
1
To run: ./hello
Hello, world!
Andre Kessler
17 / 30
Memory Model
Pointers
How do you get at this information about memory?
Through pointers; that is, the & and * operators
int a = 5; The address of a is &a.
int *a ptr = &a; Read declarations from right to left.
See it this way: *a ptr is declared to be of type int.
You can apply & to any addressable value (lvalue)
return &5;
// error:
Andre Kessler
18 / 30
Memory Model
int a = 5;
int *a ptr = &a;
&a
&a ptr
Memory Address
0x7fff6f641914
0x7fff6f641918
Value
0x????????????
0x????????????
Identier
a
a ptr
Note: denitely a 64-bit machine, since the addresses are larger than
232 .
Andre Kessler
19 / 30
Memory Model
int a = 5;
int *a ptr = &a;
&a
&a ptr
Memory Address
0x7fff6f641914
0x7fff6f641918
Value
0x000000000005
0x????????????
Identier
a
a ptr
Note: denitely a 64-bit machine, since the addresses are larger than
232 .
Andre Kessler
20 / 30
Memory Model
int a = 5;
int *a ptr = &a;
&a
&a ptr
Memory Address
0x7fff6f641914
0x7fff6f641918
Value
0x000000000005
0x7fff6f641914
Identier
a
a ptr
Note: denitely a 64-bit machine, since the addresses are larger than
232 .
Andre Kessler
21 / 30
Memory Model
C Data Types
char (8)
short (16), int (32),
long (64), long long (64+)
float (32), double (64), long double ( 80)
Andre Kessler
22/ 30
Memory Model
C Data Types
Courtesy of http://en.cppreference.com/w/cpp/language/types
Andre Kessler
23/ 30
Compiling
Development Environment
Andre Kessler
24 / 30
Compiling
25 / 30
Compiling
Andre Kessler
26 / 30
Compiling
Examples
Andre Kessler
27 / 30
Compiling
Andre Kessler
28 / 30
Compiling
Andre Kessler
29 / 30
Wrap-up
Class on Friday
Will cover oating point arithmetic, memory management, and
headers in more depth.
Questions?
Andre Kessler
30 / 30
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.