Professional Documents
Culture Documents
cpphtp4 PPT 20
cpphtp4 PPT 20
Outline
20.1
20.2
20.3
20.4
20.5
20.6
20.7
20.8
20.9
20.10
20.11
20.12
20.13
Introduction
Redirecting Input/Output on UNIX and DOS Systems
Variable-Length Argument Lists
Using Command-Line Arguments
Notes on Compiling Multiple-Source-File Programs
Program Termination with exit and atexit
The volatile Type Qualifier
Suffixes for Integer and Floating-Point Constants
Signal Handling
Dynamic Memory Allocation with calloc and realloc
The Unconditional Branch: goto
Unions
Linkage Specifications
20.1 Introduction
Several advanced topics in chapter
Many capabilities specific to OS
Especially UNIX and/or DOS
Function format
Include <cstdarg>
Use ellipsis () at end of parameter list
Must be last item in parameter list
Must be one named parameter before ellipsis
double myFunction(int i, );
va_end( myList );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
fig20_02.cpp
(1 of 3)
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using
using
using
using
std::setw;
std::setprecision;
std::setiosflags;
std::fixed;
#include <cstdarg>
=
=
=
=
37.5;
22.5;
1.7;
10.2;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cout <<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
return 0;
}
Outline
// end main
fig20_02.cpp
(2 of 3)
1
0
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Outline
// calculate average
double average( int count, ... )
Create a va_list object and
{
call macro va_start.
double total = 0;
count is the parameter
va_list list; // for storing information
needed by va_start
before the ellipsis.
va_start( list, count );
// process variable length argument list
for ( int i = 1; i <= count; i++ )
total += va_arg( list, double );
// end the va_start
va_end( list );
fig20_02.cpp
(3 of 3)
1
1
double1
double2
double3
double4
=
=
=
=
Outline
37.5
22.5
1.7
10.2
fig20_02.cpp
output (1 of 1)
1
2
int argc
Number of arguments
char *argv[]
Array of strings that contains command-line arguments
argc: 3
argv[0]: "copy"
argv[1]: "input"
argv[2]: "output"
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Outline
fig20_03.cpp
(1 of 2)
using std::cout;
using std::endl;
using std::ios;
#include <fstream>
using std::ifstream;
using std::ofstream;
else {
ifstream inFile( argv[ 1 ], ios::in );
// input file could not be opened
if ( !inFile ) {
cout << argv[ 1 ] << " could not be opened" << endl;
return -1;
}
// end if
1
5
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Outline
// output character
// read next character
} // end while
// end else
return 0;
}
// end main
1
6
Example
17
#include <cstring>
Contains prototypes of functions
We do not need to know where definitions are
18
Makefiles
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
fig20_04.cpp
(1 of 2)
using std::cout;
using std::endl;
using std::cin;
#include <cstdlib>
void print();
int main()
{
atexit( print );
//
willwith
be called
if theexit"
cout << "Enter 1 to terminateprint
program
function
program
endsnormally\n";
successfully.
<< "\nEnter 2 to terminate
program
int answer;
cin >> answer;
2
2
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Outline
// exit if answer is 1
if ( answer == 1 ) {
cout << "\nTerminating program with function exit\n";
exit( EXIT_SUCCESS );
}
// end if
fig20_04.cpp
(2 of 2)
// end main
2
3
Outline
fig20_04.cpp
output (1 of 1)
2
4
25
26
Depends on system
Reinitialize handler after it handles signal
2003 Prentice Hall, Inc. All rights reserved.
27
Explanation
SIGABRT
SIGFPE
SIGILL
SIGINT
SIGSEGV
SIGTERM
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig20_06.cpp
(1 of 3)
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <csignal>
#include <cstdlib>
#include <ctime>
void signalHandler( int );
int main()
{
signal( SIGINT, signalHandler );
srand( time( 0 ) );
3
0
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Outline
Note call to function raise.
fig20_06.cpp
(2 of 3)
// end for
return 0;
}
// end main
3
1
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Outline
// handles signal
void signalHandler( int signalValue )
{
cout << "\nInterrupt signal (" << signalValue
<< ") received.\n"
<< "Do you wish to continue (1 = yes or 2 = no)? ";
fig20_06.cpp
(3 of 3)
int response;
cin >> response;
// check for invalid responses
while ( response != 1 && response != 2 ) {
cout << "(1 = yes or 2 = no)? ";
cin >> response;
}
// end while
May be required to
reinitialize.
3
2
Outline
10
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
Interrupt signal (2) received.
Do you wish to continue (1 = yes or 2
100
20
30
40
50
60
70
80
90
fig20_06.cpp
output (1 of 1)
= no)? 1
1
2
3
4
Interrupt signal (2) received.
Do you wish to continue (1 = yes or 2 = no)? 2
3
3
Function calloc
void *calloc(size_t nelmt, size_t size)
nelmt - number of elements in array
size - size of each element
Elements initialized to 0
34
35
goto statement
goto label;
Program jumps to first statement after label
Label is an identifier and colon (start:)
goto start;
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
fig20_07.cpp
(1 of 2)
using std::cout;
using std::endl;
#include <iomanip>
using std::left;
using std::setw;
int main()
{
int count = 1;
start:
// label
3
7
28
29
30
31
32
33
34
35
1
end:
Outline
// label
fig20_07.cpp
(2 of 2)
return 0;
}
2
fig20_07.cpp
output (1 of 1)
// end main
3
10
3
8
20.12 Unions
Union
Memory that contains a variety of objects
Data members share space
Only contains one data member at a time
Conserves storage
Only the last data member defined can be accessed
Declaration same as class or struct
union Number {
int x;
float y;
} ;
Union myObject;
39
20.12 Unions
Union operations
40
20.12 Unions
Anonymous unions
No type name
Does not create a type; creates an unnamed object
Contains only public data members
integer1 = 3;
2003 Prentice Hall, Inc. All rights reserved.
41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
fig20_08.cpp
(1 of 2)
int main()
{
Number value;
// union variable
value.integer1 = 100;
cout <<
<<
<<
<<
4
2
26
27
28
29
30
31
32
33
34
35
value.double1 = 100.0;
cout <<
<<
<<
<<
Outline
fig20_08.cpp
(2 of 2)
fig20_08.cpp
output (1 of 1)
return 0;
}
// end main
4
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
fig20_09.cpp
(1 of 2)
using std::cout;
using std::endl;
int main()
{
Create an anonymous union.
// declare an anonymous union
The data members can be
// members integer1, double1 and charPtr share the same space
accessed without using a
union {
union name.
int integer1;
double double1;
char *charPtr;
};
4
4
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Outline
fig20_09.cpp
(2 of 2)
fig20_09.cpp
output (1 of 1)
// end main
1 2
3.3 4.4
Anonymous union
4
5
Linkage specifications
To link properly, tell compiler that function compiled in C
For single functions
extern "C" function prototype
extern "C"
{
function prototypes
}
2003 Prentice Hall, Inc. All rights reserved.
46