Ian said:
James Kanze wrote:
[...]
Just a fancy way of saying that in practice, I've seen more problems
with dangling pointers in C++ than with memory leaks.
Which often cause segfaults at some later stage.
Typically, you'll have no problem during any of your tests, but it will
segfault consistently at the demo in front of the important customer.
Often you still need to know the problems with ntca's when using some of
those interfaces. Some of the more, shall we say 'baroque', win32 APIs
can get you into a lot of trouble without an understanding of char
buffers.
I've never written for win32 APIs -- the only application I ever wrote
for Windows was in Java. I've not found it to be a problem with most of
the Posix API, although there are a few exceptions.
Typically, you wrap the API in a class wrapper -- the author of the
class wrapper has to deal with the problem, but no one else should.
Regretfully, this idea hasn't been followed through in the C++ standard
library.
How do you define 'All of Linux'. Is it every application written for
Linux, or just the kernel and gnu tools?
Good question. Obviously not every application, since some of my C++
code runs on Linux, and I've used Together on it. (Together is in
Java.) But Linus doesn't like C++ one little bit (and obviously, Java's
not an option for the kernel)
.
Did you implement the "+" operator with 'malloc' or 'new'?
My original String class used the functions :
perator new and
:
perator delete for memory allocation and deallocation. And reference
counting as a sort of poor mans garbage collection. (This was before
the days of multithreading, so reference counting wasn't too difficult
for low level stuff, where cycles weren't possible.)
And the Length() function, did you use strlen, or did you have some
other mechanism that kept track of the character length?
Every String class I've ever seen (including java.lang.String) used a
control block with at least:
SomeIntegerType length ;
char* text ;
Mine added a reference count (and used a fancy, low level trick to put
the text buffer immediately behind the control block, so there was only
one allocation, rather than two). Most add a capacity, so that just
adding a single character doesn't always require reallocation -- since
my String was basically immutable, and I had a separate class optimized
for building strings from characters one by one.
Note that if you want to allow your String class to contain '\0',
there's not really any alternative.