Академический Документы
Профессиональный Документы
Культура Документы
Bjarne Stroustrup
www.stroustrup.com/Programming
Dennis M. Ritchie
Overview
C and C++
Function prototypes
printf()/scanf()
Arrays and strings
Memory management
Macros
const
C/C++ interoperability
ABIs
An example
Stroustrup/PPP - Dec'13
dmr
C and C++
ken
bwk
bs
doug
C++11
C11
C++14
Stroustrup/PPP - Dec'13
C and C++
Source compatibility
Link compatibility
(Almost) all constructs that are both C and C++ have the same meaning
(semantics) in both languages
C and C++
Separate committees
Both primarily aimed at and are heavily used for hard system
programming tasks, such as
Stroustrup/PPP - Dec'13
C and C++
Alternatives
Fortran
Cobol
Lisp
C++
Java
PHP
Python
Stroustrup/PPP - Dec'13
C and C++
Here we
Our ideal of programming and our techniques remain the same, but
the tool available to express our ideas change
Stroustrup/PPP - Dec'13
C and C++
is a better C
supports data abstraction
supports object-oriented programming
supports generic programming
C:
Stroustrup/PPP - Dec'13
10
C and C++
11
Use malloc()/free()
References
new/delete
Function overloading
Use macros
Exceptions
Use pointers
Use macros
Stroustrup/PPP - Dec'13
12
Containers
STL algorithms
I/O streams
Regular expression
13
C and C++
In principle, you dont need a high-level language, you could write everything in
assembler (but why would you want to do that?)
If you think you see a difference, suspect differences in default optimizer or linker
settings
Stroustrup/PPP - Dec'13
14
Functions
Stroustrup/PPP - Dec'13
15
Function prototypes
(function argument checking is optional)
/* avoid these mistakes use a compiler option that enforces C++ rules */
int g(int);
int h();
16
int main(void)
{
printf("Hello, world\n");
return 0;
}
Arguments to be formatted
Formatting characters
Stroustrup/PPP - Dec'13
17
18
s for string
d for decimal, not double (use g for double)
19
Remember
it decays to a pointer
use memcpy()
Stroustrup/PPP - Dec'13
20
C-style strings
'a'
p:
s:
'a'
's'
's'
'd'
'd'
Stroustrup/PPP - Dec'13
'f'
'f'
21
C-style strings
Comparing strings
#include <string.h>
if (s1 = = s2) {
/* do s1 and s2 point to the same array? */
/* (typically not what you want) */
}
if (strcmp(s1,s2) = = 0) { /* do s1 and s2 hold the same characters? */
}
Copying strings
strcpy(s1,s2);
/* copy characters from s2 into s1 */
/* be sure that s1 can hold that many characters */
/* and/or use strncpy */
Stroustrup/PPP - Dec'13
22
C-style strings
23
<stdio.h>
<string.h>
<ctype.c>
<stdlib.h>
<math.h>
Stroustrup/PPP - Dec'13
24
Stroustrup/PPP - Dec'13
25
No initialization/cleanup
/* m ints */
/* initialize ints (Eh?) */
26
Uncast malloc()
Not-type safe
Historically a pre-standard C compatibility hack/feature
Always controversial
Unnecessarily so IMO
27
void*
28
// Comments
Stroustrup/PPP - Dec'13
29
const
// in C, a const is never a compile time constant
const int max = 30;
const int x;
// const not initialized: ok in C (error in C++)
void f(int v)
{
int a1[max]; // error: array bound not a constant (max is not a constant!)
int a2[x];
// error: array bound not a constant (here you see why)
switch (v) {
case 1:
//
case max:
// error: case label not a constant
//
}
}
Stroustrup/PPP - Dec'13
30
Stroustrup/PPP - Dec'13
31
Beware of macros
#include "my_header.h"
//
int max(int a, int b) { return a>=b?a:b; } // error: obscure error message
No wonder it complained!
There are tens of thousands of macros in popular header files.
Always define macros with ALL_CAPS names, e.g.
#define MY_MAX 30
Never give anything but a macro an ALL_CAPS name
Unfortunately, not everyone obeys the ALL_CAPS convention
Stroustrup/PPP - Dec'13
32
C/C++ interoperability
Optimal/Efficient
No behind-the-scenes reformatting/conversions
Stroustrup/PPP - Dec'13
33
// link as a C function
void my_c_plus_plus_fct()
{
double sr2 = sqrt(2);
//
}
Stroustrup/PPP - Dec'13
34
35
ABIs
The problem
struct file {
mode_t f_mode;
loff_t f_pos;
unsigned short f_flags;
unsigned short f_count;
unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
struct file *f_next, *f_prev; int f_owner; /* pid or -pgrp where SIGIO should be sent */
struct inode * f_inode;
struct file_operations * f_op;
unsigned long f_version;
void *private_data; /* needed for tty driver, and maybe others */
};
Stroustrup/PPP - Dec'13
36
ABI
A solution:
And NEVER use that FILE directly, just pass the FILE*
Stroustrup/PPP - Dec'13
37
ABI
C++ alternatives
Performance
Flexibility
Stroustrup/PPP - Dec'13
38
Stroustrup/PPP - Dec'13
39
40
Stroustrup/PPP - Dec'13
41
42
See, you dont need any fancy and complicated language features!!!
not my comment BS
My first reaction BS
43
More information
The classic
Stroustrup/PPP - Dec'13
44