C++ Programming HOW-TO
Al Dev (Alavoor Vasudevan) alavoor@yahoo.com
v16.0, 03 Aug 2000
This document discusses methods to avoid memory problems in C++ and
also will help you to program properly in C++ language. The informa? tion in this document applies to all the operating systems that is -
Linux, MS DOS, BeOS, Apple Macintosh OS, Microsoft Windows
95/98/NT/2000, OS/2, IBM OSes (MVS, AS/400 etc..), VAX VMS, Novell
Netware, all flavors of Unix like Solaris, HPUX, AIX, SCO, Sinix, BSD,
etc.. and to all other operating systems which support "C++" compiler
(it means almost all the operating systems on this planet!).
______________________________________________________________________
Table of Contents
1. Introduction
1.1 Problems facing the current C++ compilers
1.2 Which one "C", "C++" or Java ?
2. Download String
3. Usage of String class
3.1 Operators
3.2 Functions
4. C++ Zap (Delete) command
5. Pointers are problems
6. Usage of my_malloc and my_free
7. Debug files
8. C++ Online-Docs
8.1 C++ Tutorials
8.2 C++ Coding Standards
8.3 C++ Quick-Reference
8.4 C++ Usenet Newsgroups
9. Memory Tools
10. Related URLs
11. Other Formats of this Document
12. Copyright
13. Appendix A example_String.cpp
14. Appendix B String.h
15. Appendix C String.cpp
16. Appendix D my_malloc.cpp
17. Appendix E my_malloc.h
18. Appendix F debug.h
19. Appendix G debug.cpp
20. Appendix H Makefile
______________________________________________________________________
1. Introduction
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.
1.1. Problems facing the current C++ compilers
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 usage of char * in 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.
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 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 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 extern "C" statement says that everything within the brace-sur? rounded 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 which is given in this document and included in the STDLIB. The
utilises the constructor and destructor to automate the memory
management and also provides many functions like ltrim, substring,
etc..
See also related in the C++ compiler. The string class is part of the
standard GNU C++ library and provides lot of string manipulation
functions. The 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 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 and and there will be no char * and char[]. The compiler
should try to prevent bad programming practices!
1.2. Which one "C", "C++" or Java ?
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 "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 StrinReader
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++
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
This document discusses methods to avoid memory problems in C++ and also will help you to program properly in C++ language. The information in this document applies to all the operating systems that is - Linux, MS DOS, BeOS, Apple Macintosh OS, Microsoft Windows 95/98/NT/2000, OS/2, IBM OSes (MVS, AS/400 etc..), VAX VMS, Novell Netware, all flavors of Unix like Solaris, HPUX, AIX, SCO, Sinix, BSD, etc.. and to all other operating systems which support "C++" compiler (it means almost all the operating systems on this planet!).
资源推荐
资源详情
资源评论
收起资源包目录
c++programminghowto.zip (1个子文件)
C++Programming-HOWTO.txt 191KB
共 1 条
- 1
资源评论
JLGAO2000
- 粉丝: 0
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 从 Python 访问 Java 类.zip
- 交互式 JavaScript 沙箱.zip
- 交互式 JavaScript API 参考.zip
- 使用SSM框架的Java Web项目-电商后台管理.zip
- 与 FrontendMasters 课程 JavaScript 和 React 模式相关的 repo.zip
- win11系统有ie浏览器,打开ie浏览器自动跳转edge浏览器解决方案
- 基于Spark的新闻推荐系统源码+文档说明(高分项目)
- 27个常用分布函数详细汇总-名称+类别+用途+概率密度曲线+公式-PPT版本
- Python毕业设计基于时空图卷积ST-GCN的骨骼动作识别项目源码+文档说明(高分项目)
- 一个易于使用的多线程库,用于用 Java 创建 Discord 机器人 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功