[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ? about C++




On Thursday, March 7, 2002, at 03:57 PM, Dimitri Maziuk wrote:
Anyhow, my point was, name 4 problem areas in C.


You're lucky with 'none of the above'. It could be...

1. No array bounds checking (Fix: use vector<> or equivalent)

Real-world idiot fix: My name is 7 characters; give it twenty, we'll never need more than that! Then use that twenty inline in the code, as well as simplifying the expressions a little so we get the related constants 21 (w/ null), 19 (can't count, paranoid), 8, 9, 10 (unicode), and infinity (core dump).

2. Pointers (Fix: use references, iterators etc.).

Real-world idiot fix: Pass everything by value. Who needs pointers, references, etc.? Use indices with magic constants as the bounds (see above) to iterate arrays.

BTW: I disagree _strongly_ that pointers are a misfeature of C.
	They are a very useful tool. However, misuse of pointers
	is a misfeature of some programmers.

3. Forgetting to free() malloc()'ed memory (Fix: use auto_ptr<>,
   destructors etc.)

Real-world idiot fix: Buy more memory. If that proves impossible, increase amount of disk used for swap. In the event of hitting architectural limits, go 64-bit. Or just make the program crash more often. Then it won't leak as much memory. Tell the user to save often.

4. Using pointers to (hopefully array) (hopefully allocated) (hopefully
   null-terminated) of char as string data type (see also 1, 2, & 3)
   (Fix: use std::string)

Real-world idiot fix: Add 'hopefully not overflowed' to the list. Fix allocation problems by allocating on the stack. If worried about array not being large enough, increase static size. See #1. If worried about excess stack usage, see #3. And, for the hell of it, see #4 ;-)



Reply to: