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 20 to 30 times FASTER than Java. Java runs very slow because it is an byte-code-interpreted language running on top of "virtual engine". 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" 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 usage of char * 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 a million lines of code.
Hence, the following techniques are proposed to overcome the faults of "C" language.
It is proposed that C++ compilers should prevent the programmers from using the "char *" , "char[]" datatypes and functions like strcpy, strcat, strncpy, strncat. The datatypes like char *, char[] and functions like strcpy, strcat are evil and must be completetly BANNED from usage in C++!! The "char *" is like smallpox virus and it must be eradicated from this world!!
Instead of using char * and char[] all the C++ programmers MUST use the 'mychar class' which is given in this document and 'string class' included in the STDLIB. The 'mychar 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 'mychar class' can remove the need of char * datatype. Also, C++ programmers must be encouraged to use 'new', 'delete' features instead of using 'malloc' or 'free'.
The 'mychar 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!!
The GNU C++ compiler MUST drop off the support of char *, char[] datatypes and in order to compile older programs using char datatype, the compiler should provide a additional option called "-fchar-datatype" to g++ command. Over the next 2 years all the C++ programs will use 'mychar class' and 'string class' and there will be no char * and char[]. The compiler should try to prevent bad programming practices!
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 compiler optimizer options like -O or -O3 can speed up C++.
Nowadays, "C" language is primarily used for "systems programming" to develop operating sytems, device drivers etc..
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 is under your control and never changes and you will never know what the client side web-browser OS is. It can be Windows 95/98/NT/2000 or Linux, Apple Mac, OS/2, Solaris etc..