Adding Java and C++ to the MQTT benchmarks or: How I Learned to Stop Worrying and Love the Garbage Collector

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jan 9 11:24:50 PST 2014


On Thu, Jan 09, 2014 at 07:08:42PM +0000, digitalmars-d-bounces at puremagic.com wrote:
> On Thursday, 9 January 2014 at 18:34:58 UTC, Walter Bright wrote:
> >On 1/9/2014 10:18 AM, "Ola Fosheim Grøstad"
> >>Why would you do that? You would have to overload cat then.
> >
> >So you agree that it won't work.
> 
> It will work for string literals or for malloc'ed strings, but not
> for both using the same function unless you start to depend on the
> data sections used for literals (memory range testing). Which is a
> dirty tool-dependent hack.
> 
> >Overloading doesn't work because a string literal and a string
> >allocated by something else have the same type.
> 
> Not if you return your own type, but have the same structure? You
> return a struct, containing a variabled sized array of char, and
> overload on that?
> 
> But I see your point regarding literal/malloc, const char* and char*
> is a shady area, you can basically get anything cast to const char*.

And since it is C, people expect to pass char* and const char* around.
So most likely what will happen is that if there's any way at all to get
a char* or const char* out of your opaque struct, they will do it, and
then pass it to strcat, strlen, and who knows what else. You can't
really stop this except by convention, because the language doesn't
enforce the encapsulation, and making it truly opaque (via void* with
PIMPL) will require an extra layer of indirection and make it unusable
with commonly-expected C APIs like printf.

But we all know what happens with programming by convention when the
team grows bigger -- old people who know the Right Way of doing things
leave, and new people come in ignorant of how things are Supposed To Be,
falling back to const char*, so the code quickly degenerates into a
horrible mess of mixed conventions and memory leaks / pointer bugs
everywhere. Then you start strdup'ing everything Just In Case. Which was
Walter's original point.


T

-- 
By understanding a machine-oriented language, the programmer will tend to use a much more efficient method; it is much closer to reality. -- D. Knuth


More information about the Digitalmars-d mailing list