Re: Can Java Programmer Learn C++ Quickly?
From: James Kanze (kanze_at_none)
Date: 12/11/04
- Next message: Joona I Palaste: "Re: Can Java Programmer Learn C++ Quickly?"
- Previous message: James Kanze: "Re: Can Java Programmer Learn C++ Quickly?"
- In reply to: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Next in thread: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Reply: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 11 Dec 2004 19:23:05 +0100
Ian T wrote:
> 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.
>> It's true that C++ often interfaces directly to C level API's. And of
>> course, you have to use null terminated strings there. But that use is
>> generally limited to the interface level itself.
> 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.
>> All of Linux is in C, for example.
> 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):-).
>> The sources of java.lang.String are available somewhere. My
>> pre-standard String class (dating from 1991) looked almost exactly like
>> it -- the main difference was that I used reference counting for garbage
>> collection, and I supported value semantics (but the only way to modify
>> a String originally was assignment -- I had a StringBuilder class which
>> worked a lot like StringBuffer).
> Did you implement the "+" operator with 'malloc' or 'new'?
My original String class used the functions ::operator new and
::operator 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.
--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
- Next message: Joona I Palaste: "Re: Can Java Programmer Learn C++ Quickly?"
- Previous message: James Kanze: "Re: Can Java Programmer Learn C++ Quickly?"
- In reply to: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Next in thread: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Reply: Ian T: "Re: Can Java Programmer Learn C++ Quickly?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|