Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Chapter 18

That’s it! The only other required piece is the actual C++ program. Implementation of a cryptographic algorithm is beyond the scope of this book. The important piece is the main() function because it accepts the string that should be encrypted as an argument.

Arguments are contained in the argv array of C-style strings. You should always consult the argc parameter before accessing an element of argv. Remember that if argc is 1, there is one element in the argument list and it is accessible at argv[0]. The 0th element of the argv array is generally the name of the program, so actual parameters begin at argv[1].

Following is the main() function for a C++ program that encrypts the input string. Notice that the program returns 0 for success and non-0 for failure, as is standard in Unix.

int main(int argc, char** argv)

{

if (argc < 2) {

cerr << “Usage: “ << argv[0] << “ string-to-be-encrypted” << endl; return -1;

}

cout << encrypt(argv[1]);

return 0;

}

There is actually a blatant security hole in this code. When the to-be-encrypted string is passed to the C++ program as a command-line argument, it may be visible to other users through the process table. A more secure way to get the information into the C++ program would be to send it through standard input, which is the forte of the expect scripting language.

Now that you’ve seen how easily C++ programs can be incorporated into scripting languages, you can combine the strengths of the two languages for your own projects. You can use a scripting language to interact with the OS and control the flow of the script, and a traditional programming language for the heavy lifting.

Mixing C++ with Assembly Code

C++ is generally considered a fast language, especially relative to other object-oriented languages. Yet, there is simply no way to beat raw assembly code when speed is absolutely critical. Recall that when your program is compiled, it is turned from high-level C++ into low-level assembly. The automatically generated assembly code is fast enough for most purposes. Optimizers are often run over the generated assembly code to make it even faster. Yet, for all the advances in compiler writing, a talented human being can often write assembly code that outperforms compiled C++ code.

In C++, the keyword asm is used by many compilers to allow the programmer to insert raw assembly code. The keyword is part of the C++ standard, but its implementation is compiler-defined. In most compilers, you can use asm to drop from C++ down to the level of assembly right in the middle of your program.

Inline assembly can be very useful in certain applications, such as intensive 3-D graphics, but we don’t recommend it for most programs. There are several reasons to avoid inline assembly code:

504

Developing Cross-Platform and Cross-Language Applications

Your code is no longer portable to another processor once you start including raw assembly code for your platform.

Most programmers don’t know assembly languages and won’t be able to modify or maintain your code.

Assembly code is not known for its readability. It can hurt your program’s use of style.

Most of the time, it is simply not necessary. If your program is slow, look for algorithmic problems or consult some of the other performance suggestions in Chapter 17.

Summar y

If you take away one point from this chapter, it should be that C++ is a flexible language. It exists in the sweet spot between languages that are too tied to a particular platform and languages that are too highlevel and generic. Rest assured that when you develop code in C++, you aren’t locking yourself into the language forever. C++ can be mixed with other technologies and has a solid history and code base that help guarantee its relevance in the future.

505