mirror of https://github.com/tLDP/LDP
updated
This commit is contained in:
parent
d2bd0f9b2d
commit
c29e7fb549
|
@ -40,7 +40,7 @@ C++ Programming HOW-TO
|
||||||
<author>Al Dev (Alavoor Vasudevan)
|
<author>Al Dev (Alavoor Vasudevan)
|
||||||
<htmlurl url="mailto:alavoor@yahoo.com"
|
<htmlurl url="mailto:alavoor@yahoo.com"
|
||||||
name="alavoor@yahoo.com">
|
name="alavoor@yahoo.com">
|
||||||
<date>v8.0, 26 April 2000
|
<date>v9.0, 31 May 2000
|
||||||
<abstract>
|
<abstract>
|
||||||
This document discusses methods to avoid memory problems in C++ and
|
This document discusses methods to avoid memory problems in C++ and
|
||||||
also will help you to program properly in C++ language.
|
also will help you to program properly in C++ language.
|
||||||
|
@ -71,8 +71,12 @@ means almost all the operating systems on this planet!).
|
||||||
<p>
|
<p>
|
||||||
C++ is the most popular language and will be used for a long time in the future
|
C++ is the most popular language and will be used for a long time in the future
|
||||||
inspite of emergence of Java. C++ runs <bf>extremely fast</bf> and is
|
inspite of emergence of Java. C++ runs <bf>extremely fast</bf> and is
|
||||||
in fact <bf> 20 to 30 times FASTER than </bf> Java. Java runs very slow
|
in fact <bf> 10 to 20 times FASTER than </bf> Java. Java runs very slow
|
||||||
because it is an byte-code-interpreted language running on top of "virtual engine". The
|
because it is an byte-code-interpreted language running on top of "virtual engine".
|
||||||
|
Java runs faster with JIT compiler but is still slower than C++. And optimized
|
||||||
|
C++ program is
|
||||||
|
about <bf>3 to 4 times faster</bf> than Java
|
||||||
|
using the JIT (Just-In-Time) compiler!! The
|
||||||
memory management in Java is automated, so that programmers do not directly
|
memory management in Java is automated, so that programmers do not directly
|
||||||
deal with memory allocations. This document attempts
|
deal with memory allocations. This document attempts
|
||||||
to automate the memory management in C++ to make it much more easy to use.
|
to automate the memory management in C++ to make it much more easy to use.
|
||||||
|
@ -104,7 +108,8 @@ to usage of features like -
|
||||||
|
|
||||||
The usage of <bf>char *</bf> and <bf>strcpy</bf> causes <it>horrible</it> memory
|
The usage of <bf>char *</bf> and <bf>strcpy</bf> causes <it>horrible</it> memory
|
||||||
problems due to <it>"overflow"</it>,
|
problems due to <it>"overflow"</it>,
|
||||||
<it>"fence past errors"</it> or <it>"memory leaks"</it>.
|
<it>"fence past errors"</it>, <it>"step-on-others-toe"</it>
|
||||||
|
(hurting other variable's memory locations) or <it>"memory leaks"</it>.
|
||||||
The memory problems are extremely hard to debug and are
|
The memory problems are extremely hard to debug and are
|
||||||
very time consuming to fix and trouble-shoot. Memory problems bring down
|
very time consuming to fix and trouble-shoot. Memory problems bring down
|
||||||
the productivity of programmers. This document helps in increasing the
|
the productivity of programmers. This document helps in increasing the
|
||||||
|
@ -114,12 +119,12 @@ 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
|
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
|
bugs will be "hiding" in the code for several months and can cause unexpected
|
||||||
program crashes!! The usage of
|
program crashes!! The usage of
|
||||||
<bf>char *</bf>
|
<bf>char *</bf> in C++
|
||||||
is costing USA and Japan $2 billion
|
is costing USA and Japan $2 billion
|
||||||
every year in time lost in debugging and downtime of programs. If you use
|
every year in time lost in debugging and downtime of programs. If you use
|
||||||
<bf>char *</bf>
|
<bf>char *</bf>
|
||||||
in C++ then it is a very costly affair especially if your programs have more
|
in C++ then it is a very costly affair especially if your programs have more
|
||||||
than a million lines of code.
|
than 50,000 lines of code.
|
||||||
|
|
||||||
Hence, the following techniques are proposed to overcome the faults of "C"
|
Hence, the following techniques are proposed to overcome the faults of "C"
|
||||||
language.
|
language.
|
||||||
|
@ -134,7 +139,24 @@ The datatypes like char *, char[] and functions like strcpy, strcat
|
||||||
are <bf>evil</bf> and must be completetly <bf>BANNED</bf> from usage in C++!!
|
are <bf>evil</bf> and must be completetly <bf>BANNED</bf> from usage in C++!!
|
||||||
The
|
The
|
||||||
<bf>"char *"</bf>
|
<bf>"char *"</bf>
|
||||||
is like <it>smallpox virus</it> and it must be eradicated from this world!!
|
is like <it>smallpox virus</it> and it must be eradicated from C++ world!!
|
||||||
|
If you want to use "char *" as in some system functions than you
|
||||||
|
should use "C" language. You would put
|
||||||
|
all your "C" programs in a seperate file and link to "C++" programs using
|
||||||
|
the <it>linkage-specification</it> statement <bf>extern "C" </bf> -
|
||||||
|
<code>
|
||||||
|
extern "C" {
|
||||||
|
#include <stdlib.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
comp();
|
||||||
|
some_c_function();
|
||||||
|
}
|
||||||
|
</code>
|
||||||
|
The <bf>extern "C"</bf> statement says that everything within the
|
||||||
|
brace-surrounded block - in this case, everything in the header file
|
||||||
|
and comp(), some_c_function() is compiled by a C compiler.
|
||||||
|
|
||||||
Instead of using char * and char[] all the C++ programmers MUST use the
|
Instead of using char * and char[] all the C++ programmers MUST use the
|
||||||
<bf>'mychar class'</bf>
|
<bf>'mychar class'</bf>
|
||||||
|
@ -189,7 +211,15 @@ It is recommended you do programming in object-oriented "C++" for all your
|
||||||
application programming or general purpose programming. You can take full
|
application programming or general purpose programming. You can take full
|
||||||
advantage of object oriented facilities of C++. The C++ compiler is lot
|
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"
|
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++.
|
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
|
Nowadays, "C" language is primarily used for "systems programming" to develop
|
||||||
operating sytems, device drivers etc..
|
operating sytems, device drivers etc..
|
||||||
|
@ -201,7 +231,14 @@ XML to get better performance. Hence, the golden rule is <it>"Web-server side pr
|
||||||
use C++ and web-client side (browser) programming use Java applets"</it>. The
|
use C++ and web-client side (browser) programming use Java applets"</it>. The
|
||||||
reason is - the server-side OS is under your control and never changes and you
|
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
|
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..
|
or Linux, Apple Mac, OS/2, Netware, Solaris etc..
|
||||||
|
|
||||||
|
The greatness of Java is that it can run "GUI Applets" on any OS platform!
|
||||||
|
Java was created to replace the Microsoft Windows 95/NT GUI clients.
|
||||||
|
In other words - "Java is the Windows-GUI system of next century". Java
|
||||||
|
is already embedded in web-browsers like Netscape, Hot Java, etc..
|
||||||
|
|
||||||
|
Hence, Java runs on "client" and C++ runs on servers!!
|
||||||
<!--
|
<!--
|
||||||
*******************************************
|
*******************************************
|
||||||
************ End of Section ***************
|
************ End of Section ***************
|
||||||
|
@ -489,6 +526,10 @@ For an example, see the file "mychar.h" which is using
|
||||||
the
|
the
|
||||||
<ref id="my_malloc" name="my_malloc()">
|
<ref id="my_malloc" name="my_malloc()">
|
||||||
and my_free() functions.
|
and my_free() functions.
|
||||||
|
|
||||||
|
<bf>WARNING :</bf> Do not use free() to free memory allocated with 'new'
|
||||||
|
or 'delete' to free memory allocated with malloc. If you do, then
|
||||||
|
results will be unpredictable!!
|
||||||
<!--
|
<!--
|
||||||
*******************************************
|
*******************************************
|
||||||
************ End of Section ***************
|
************ End of Section ***************
|
||||||
|
@ -501,15 +542,18 @@ and my_free() functions.
|
||||||
-->
|
-->
|
||||||
<sect> Pointers are problems <label id="pointers">
|
<sect> Pointers are problems <label id="pointers">
|
||||||
<p>
|
<p>
|
||||||
|
Pointers are not required for general purpose programming. In modern
|
||||||
|
languages like Java there is no support for pointers!! Pointers make
|
||||||
|
the programs messy and programs using pointers are very hard to read.
|
||||||
|
|
||||||
|
Avoid using pointers as much as possible and use references. Pointers are really a
|
||||||
|
great pain. It is possible to write a application without using pointers.
|
||||||
|
|
||||||
A <bf>reference</bf> is an alias; when you create a reference, you initialize
|
A <bf>reference</bf> is an alias; when you create a reference, you initialize
|
||||||
it with the name of another object, the target. From the moment on, the reference
|
it with the name of another object, the target. From the moment on, the reference
|
||||||
acts as an alternative name of the target, and anything you do to the reference
|
acts as an alternative name of the target, and anything you do to the reference
|
||||||
is really done to the target.
|
is really done to the target.
|
||||||
|
|
||||||
Avoid using pointers as much as possible and use references. Pointers are really a
|
|
||||||
great pain. It is possible to write a application without using pointers.
|
|
||||||
In languages like Java, pointers are not supported at all !!
|
|
||||||
|
|
||||||
<bf>Syntax of References:</bf> Declare a reference by writing the type, followed by
|
<bf>Syntax of References:</bf> Declare a reference by writing the type, followed by
|
||||||
the reference operator (&), followed by the reference name. References
|
the reference operator (&), followed by the reference name. References
|
||||||
<bf>MUST</bf> be initialized at the time of creation.
|
<bf>MUST</bf> be initialized at the time of creation.
|
||||||
|
@ -897,6 +941,8 @@ Additional requests are - you must retain the author's name, email address
|
||||||
and this copyright notice on all the copies. If you make any changes
|
and this copyright notice on all the copies. If you make any changes
|
||||||
or additions to this document then you please
|
or additions to this document then you please
|
||||||
intimate all the authors of this document.
|
intimate all the authors of this document.
|
||||||
|
Brand names mentioned in this document are property of their respective
|
||||||
|
owners.
|
||||||
<!--
|
<!--
|
||||||
*******************************************
|
*******************************************
|
||||||
************ End of Section ***************
|
************ End of Section ***************
|
||||||
|
|
|
@ -187,7 +187,7 @@ device parameters.
|
||||||
How to install, configure and use a busmouse under Linux.
|
How to install, configure and use a busmouse under Linux.
|
||||||
|
|
||||||
<item><htmlurl url="C++Programming-HOWTO.html" name="C++Programming-HOWTO">,
|
<item><htmlurl url="C++Programming-HOWTO.html" name="C++Programming-HOWTO">,
|
||||||
<BF/C++ Programming HOWTO/ <p><em/Updated: April 2000/.
|
<BF/C++ Programming HOWTO/ <p><em/Updated: May 2000/.
|
||||||
Discusses methods to avoid memory problems in C++ and
|
Discusses methods to avoid memory problems in C++ and
|
||||||
also will help you to program properly in C++ language.
|
also will help you to program properly in C++ language.
|
||||||
Applies to all operating sytems.
|
Applies to all operating sytems.
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue