C++ is the most popular language and will be used for a long time in the future inspite of emergence of Java. C++ runs extremely fast and is in fact 10 to 20 times FASTER than Java. Java runs very slow because it is an byte-code-interpreted language running on top of "virtual machine". Java runs faster with JIT compiler but is still slower than C++. And optimized C++ program is about 3 to 4 times faster than Java using the JIT (Just-In-Time) compiler. The memory management in Java is automated, so that programmers do not directly deal with memory allocations. This document attempts to automate the memory management in C++ to make it much more easy to use. A neat feature of Java is that memory allocations are taken care of automatically. This howto will enable "C++" to "compete/imitate" with Java language in memory management.
Because of manual memory allocations, debugging the C++ programs consumes a major portion of time. The information in this document will give you some better ideas and tips to reduce the debugging time.
Since C++ is super-set of C, it got all the bad features of "C" language.
For example, in "C" programming - memory leaks, memory overflows are very common due to usage of features like -
Datatype char * and char[]
String functions like strcpy, strcat, strncpy, strncat, etc..
Memory functions like malloc, realloc, strdup, etc..
The usage of char * and strcpy causes horrible memory problems due to "overflow", "fence past errors", "step-on-others-toe" (hurting other variable's memory locations) or "memory leaks". The memory problems are extremely hard to debug and are very time consuming to fix and trouble-shoot. Memory problems bring down the productivity of programmers. This document helps in increasing the productivity of programmers via different methods addressed to solve the memory defects in "C++". Memory related bugs are very tough to crack, and even experienced programmers take several days, weeks or months to debug memory related problems. Many times memory bugs will be "hiding" in the code for several months and can cause unexpected program crashes. The memory bugs due to usage of char * in C/C++ is costing USA and Japan $2 billion every year in time lost in debugging and downtime of programs. If you use char * in C++ then it is a very costly affair especially if your programs have more than 50,000 lines of code.
Hence, the following techniques are proposed to overcome the faults of "C" language.
You should use the Java style String class given in this howto or STDlib string class whereever possible and limit the usage of char * to cases where you cannot use the String class. The rule is - give first preference to String class and if String class will not do the job then use the char * in C++.
Another technique is to use "C" language using "char *" and you would put all your "C" programs in a separate file and link to "C++" programs using the linkage-specification statement extern "C" -
extern "C" {
#include <stdlib.h>
}
extern "C" {
comp();
some_c_function();
}
The 'String class' utilises the constructor and destructor to automate the memory management and also provides many functions like ltrim, substring, etc..
See also related 'string class' in the C++ compiler. The string class is part of the standard GNU C++ library and provides lot of string manipulation functions. The 'string class' and 'String class' can limit the usage of char * datatype. Also, C++ programmers must be encouraged to use 'new', 'delete' features instead of using 'malloc' or 'free'.
The 'String class' does everything that char * or char [] does. It can completely replace char datatype. Plus added benefit is that programmers do not have to worry about the memory problems and memory allocation at all.
"C++" is the super-set of "C" and it is proposed that super-set of "C++" be named COOP which is acronym for " C++ O bject O riented P rogramming-language" pronounced as "koooop". The "C" files have *.c as file extenstions, "C++" have *.cpp and it is proposed that "COOP" have *.coo as file extenstions. There is a need for "COOP" because I found some programs which were written in C++ but never used Object oriented techniques. Also programs written in C++ are very hard to maintain as someone can write "C" like programs with C++. The following can be the features of COOP -
In order implement this "COOP" borrow ideas from -
COOP will compete with proprietary Microsoft C# and Sun's Java langauges. Microsoft C# overview is at - http://msdn.microsoft.com/vstudio/nextgen/technology/csharpintro.asp. Microsoft should have named C# as "COOP" because "coop" is a better name than C# and more easy to say and files can have *.coo as extension. Also take a look at Connective C++ at http://www.quintessent.com/products/cc++.
It is recommended you do programming in object-oriented "C++" for all your application programming or general purpose programming. You can take full advantage of object oriented facilities of C++. The C++ compiler is lot more complex than "C" compiler and C++ programs may run bit slower than "C" programs. But speed difference between "C" and "C++" is very minute - it could be few milli-seconds which may have little impact for real-time programming. Since computer hardware is becoming cheaper and faster and memory 'RAM' is getting faster and cheaper, it is worth doing code in C++ rather than "C" as time saved in clarity and re-usability of C++ code offsets the slow speed. Compiler optimizer options like -O or -O3 can speed up C++/C which is not available in Java.
Nowadays, "C" language is primarily used for "systems programming" to develop operating systems, device drivers etc..
Note: Using the String, StringBuffer, StringTokenizer and StringReader classes given in this howto, you can code in C++ which "exactly" looks like Java. This document tries to close the gap between C++ and Java, by imitating Java classes in C++
Java is platform independent language more suitable for developing GUI running inside web-browsers (Java applets) but runs very slow. Prefer to use web-server-side programming "Fast-CGI" with C++ and HTML, DHTML, XML to get better performance. Hence, the golden rule is "Web-server side programming use C++ and web-client side (browser) programming use Java applets". The reason is - the server-side OS (Linux) is under your control and never changes, but you will never know what the client side web-browser OS is. It can be Internet appliance device (embedded linux+netscape) or computers running Windows 95/98/NT/2000 or Linux, Apple Mac, OS/2, Netware, Solaris etc..
The advantage of Java language is that you can create "Applets (GUI)" which can run on any client OS platform. Java was created to replace the Microsoft Windows 95/NT GUI APIs like MS Visual Basic or MS Visual C++. In other words - "Java is the cross-platform Windows-GUI API language of next century". Many web-browsers like Netscape supports Java applets and web-browser like Hot Java is written in java itself. But the price you pay for cross-platform portability is the performance, applications written in Java run very slow.
Hence, Java runs on "client" and C++ runs on servers.
|
Закладки на сайте Проследить за страницей |
Created 1996-2026 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |