- BOOKS:
- 0. Starting out with C++
by Tony Gaddis (Publisher:Scott Jones).
Seems well regarded.
- 1. Programming in C++
by Dewhurst + Stark, Prentice-Hall
-- has section on creating C++ libraries
- 2. C++ Complete (on C++ standard)
by Anthony Rudd
- 3. The standard C++ Library
by Plauger, Prentice Hall, $38
-- clearly useful!
- 4. Efficient C/C++ Programming
by Heller, AP Professional, $40
-- clearly I should read if I want our project
to be meaningful
- 5. Jump Start to C programming and the UNIX interface
by Derek Kiong Beng Kee
- INTERACTION WITH COMPILER:
-
% CC // produces a.out file
% CC -o // produces
% CC -g // to keep debugging info
- Online help:
The help for the Sun's CC compiler can be found under
http://cims:8888
- For debugger options, go here:
http://cims:8888/ab2/coll.32.5/CPPPUG/@Ab2PageView/2674?
- Gnu C++ is invoked as
% g++ -o
This may abort if I do not have enough swap space.
The following seems sufficient:
% /usr/etc/pstat -s
10844k allocated + 2980k reserved = 13824k used, 6380k available
- COMMENTS:
- One line comment: // ...
- Multiline comment: /* ... */
- Boolean expressions,
- 3.0 make sure you use "==" for equality test
- 3.1 zero = false, non-zero = true
- 3.2 NULL pointer is really zero
- 3.3 Boolean operators (or, and, not): ||, &&, !
- BigNumber package (for g++, not CC): include the Integer.h file. E.g.
- Delete:
E.g. delete Iptr; // deletes a single variable
But if Iptr points to a dynamically allocated array, the symbol
[] must be inserted:
delete [] Iptr;
NOTE: be sure to ONLY use pointers with "delete"
that were previously used with "new". (From Gaddis book, p.553)
NOTE: if destructors (e.g. ~Polynomial) is wrongly
done, you can get mysterious errors and core dumps at the
END of the program! This
is because the destructors are called at the programs
- MANUAL pages.
- Random Numbers
- srand() -- see below, item 15.
- drand() -- when you need more precision in random numbers...
(As in the Hashing FKS implementation by Grech...)
-
Ellipsis
See p.134 of Stroustrup for working with ellipsis:
Need to include < stdarg.h >.
E.g. (in my design for integer expression package)
Integer Expr::ExprEval(const Expr & e, ErrBd relErr, ErrBd absErr ...)
{
va_list ap;
Param x;
Integer n;
// set up the arguments for evaluation
va_start(ap,absErr); // arg startup
for (;;) {
x = va_arg(ap,Param);
if (x==0) break;
n = va_arg(ap,Integer);
if (n==0) break;
x.Assign(n);
}
va_end(ap);
}
SPECIAL TOPICS
-
Timing tests:
see the file "timing.cc", modified from Glauner.
-
Command line arguments and main():
- 15.1 if main does not return anything and does
not need command line arguments, do
void main(void) {
...
}
- 15.2 Alternatively:
int main(int argc, char *argv[]) {
if (argc == 1)
cout<< "argc==1"; srand(200); // use 200 as random seed
else
srand(atol(argv[1]));
if (sizeof(long) < 4) {
cout << "Sorry but 'long' type must be at least 4 bytes\n";
return 0;
}
if (argc >= 3)
r=new FKS(23,argv[2]); // fks [seed] [filename]
else
r=new FKS(23,"dic.dat",70111); // fks [seed] or fks
return 1; // non-error return
} // main
- REMARK: argc is always at least 1. If argc==1, then
there are no command line arguments. Furthermore,
argv[argc-1] is the last argument.
E.g., argv[argc] gives segmentation fault!
-
STL:
-
WHY IS STL GOOD? It is the difference between
parameterized data structures (a.k.a. generic structures)
verses
derived data structures.
The latter has run time penalty cost, in terms of
virtual function calls. The former does not
have this cost.
-
SGI's standard template library for C++ classes and templates:
a resource for developers, etc.
http://www.sgi.com/Technology/STL/
It has all the basic data structures and algorithms
found in computer science (hashing, map, associative memory,
sorting, memory management, etc).
-
It is locally installed. Just include
< stl.h > or < stl >.
- Threads:
do "man thr_create".
- TCP/IP:
do "man tcp".
- Limits (max float, min integer, etc):
See the file: /usr/include/limits.h
You can include these definitions using
#include < limits.h >
-
Standard Libraries
#include < stdlib.h > -- when to use this?
#include < iostream.h >
-
Strings
If you need to get the char* inside a std::string,
use its "data()" function:
std::string s = "abc";
char * c = s.data();
-
Templates
If you have static members inside a template class,
you need to declare it outside the class before use:
template
Poly {
static int my_static; // for int, you could initialize it here!
...
}
template
int Poly::my_static = 3;
More on templates,
from http://www.cplusplus.com/doc/tutorial/templates/.
-
Streams
To use input/output file Streams,
#include
std::ifstream ifs("inputfile");
std::ofstream ofs;
ofs.open("outputfile"); // if s is a string, then
// use ofs.open(s.data());
ofs.close(); // graceful exit
...
GO TO TOP
Please send comments
to yap at cs dot nyu dot edu.