Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ANSI/ISO C++ Professional Programmer''''s Handbook phần 10 ppsx
Nội dung xem thử
Mô tả chi tiết
asm
{
mov a, ecx
//...
}
Interacting with the Operating System Directly
API functions and classes enable you to interact with the operating system. Sometimes, however, executing a system
command directly can be much faster. For this purpose, you can use the standard function system() that takes a
shell command as a const char *. For example, on a DOS/Windows system, you can display the files in the
current directory as follows:
#include <cstdlib>
using namespace std;
int main()
{
system("dir"); //execute the "dir" command
}
Here again, the tradeoff is between speed on the one hand and portability and future extensibility on the other hand.
Conclusions
In an ideal world, software designers and developers might focus their efforts on robust, extensible, and readable
code. Fortunately, the current state of affairs in the software world is much closer to that ideal than it was 15, 30, or
50 years ago. Notwithstanding that, performance tuning and optimizations will probably remain a necessity for a long
time. The faster hardware becomes, the more the software that runs on it is required to meet higher demands. Speech
recognition, online translation of natural languages, neural networks, and complex mathematical computations are
only a few examples of resource-hungry applications that will evolve in the future and require careful optimizations.
Textbooks often recommend that you put off optimization consideration to the final stages of testing. Indeed, the
primary goal is to get the system to work correctly. Nonetheless, some of the techniques presented here -- such as
declaring objects locally, preferring prefix to postfix operators, and using initialization instead of assignment -- need
to become a natural habit. It is a well-known fact that programs usually spend 90% of their time executing only 10%
of their code (the numbers might vary, but they range between 80% and 20% to 95% and 5%). The first step in
optimization is, therefore, identifying that 10% of your programs and optimizing them. Many automated profiling and
optimization tools can assist you in identifying these critical code parts. Some of these tools can also suggest solutions
to enhance performance. Still, many of the optimization techniques are implementation-specific and always require
human expertise. It is important to empirically verify your suspicions and to test the effect of suggested code
modifications to ensure that they indeed improve the system's performance. Programmers' intuitions regarding the
cost of certain operations are often misleading. For example, shorter code is not necessarily faster code. Similarly,
writing convoluted code to avoid the cost of a simple if statement is not worth the trouble because it saves only one
or two CPU cycles.
Contents
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 12 - Optimizing Your Code
file:///D|/Cool Stuff/old/ftp/1/1/ch12/ch12.htm (21 von 22) [12.05.2000 14:46:37]
© Copyright 1999, Macmillan Computer Publishing. All rights reserved.
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 12 - Optimizing Your Code
file:///D|/Cool Stuff/old/ftp/1/1/ch12/ch12.htm (22 von 22) [12.05.2000 14:46:37]
ANSI/ISO C++ Professional Programmer's
Handbook
Contents
13
C Language Compatibility Issues
by Danny Kalev
● Introduction
Differences Between ISO C and the C Subset of ANSI/ISO C++
❍ Function Parameter List
❍ Function Declaration
❍ Empty Parameter List
❍ Implicit int Declarations
❍ ISO C is currently being revised to disallow implicit int declarations.Repeated Declarations of Global Variables
❍ Implicit Casting of void Pointers
❍ The Underlying Representation of NULL Pointers
❍ Default Linkage Type of Global const Variables
❍ Null-Terminated Character Arrays
❍ Assignment of Integers to an enum Type
❍ Definition of Structs in a Function Parameter List and Return Type
❍ Bypassing an Initialization
●
Quiet Differences Between C and C++
❍ The Size of an enum Type
❍ The Size of A Character Constant
❍ Predefined Macros
❍ Default Value Returned from main()
●
Migrating From C to C++
❍ Function Wrappers
●
● Designing Legacy Code Wrapper Classes
● Multilingual Environments
C and C++ Linkage Conventions
❍ Forcing C Linkage on A C++ Function
●
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 13 - C Language Compatibility Issues
file:///D|/Cool Stuff/old/ftp/1/1/ch13/ch13.htm (1 von 20) [12.05.2000 14:46:46]
❍ Calling C++ Code from C Code
❍ Compiling main()
● Minimize the Interface Between C and C++ Code
● Mixing <iostream> Classes with <stdio.h> Functions
Accessing a C++ Object in C Code
❍ The Underlying Representation of an Object in Memory
❍ The C++ Object Model is Efficient
❍ Memory Layout of Derived Objects
❍ Support for Virtual Member Functions
❍ Virtual Inheritance
❍ Different Access Specifiers
●
● Conclusions
Introduction
C is a subset of C++. Theoretically, every valid C program is also a valid C++ program. In practice, however, there are some
subtle incompatibilities and silent differences between the seemingly common portion of both languages. Most of these
differences can be diagnosed by the compiler. Others are more evasive and, in rare conditions, they can have surprising effects.
Although it seems that most of the time legacy C code is combined with newer C++ code, the opposite is also true: C++ code
is used in C-based applications. For example, transaction-processing monitors of relational databases that are written in C
interact with code modules that are written in C++. This chapter first discusses the differences between ISO C and the C subset
of ANSI/ISO C++, and it demonstrates how to migrate legacy C code to a C++ environment. Next, you will explore the
underlying object model of C++, including the memory layout of objects, member functions, virtual member functions, virtual
base classes, and access specifiers, and you will learn how C code can access C++ objects.
Differences Between ISO C and the C Subset of ANSI/ISO C++
With a few minor differences, C++ is a superset of C. The following sections outline the differences between the C subset of
C++ and ISO C.
Function Parameter List
In pre-Standard C, the parameter list of a function was declared as follows:
/* pre-standard C, still valid in ISO C, invalid in C++*/
int negate (n)
int n; /* parameter declaration appears here*/
{
return -n;
}
In other words, only the parameters' names appeared in the parentheses, whereas their types were declared before the opening
brace. Undeclared parameters defaulted to int. In ISO C, as in C++, both the names and types of the parameters must appear
in the parentheses:
/* ISO C and C++ */
int negate (int n)
{
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 13 - C Language Compatibility Issues
file:///D|/Cool Stuff/old/ftp/1/1/ch13/ch13.htm (2 von 20) [12.05.2000 14:46:46]