Group: comp.lang.c++
From: Juha Nieminen
Date: Wednesday, April 09, 2008 6:11 PM
Subject: Re: C++ more efficient than C?

Richard Heathfield wrote:
> typedef struct point_ point;
>
> point *point_make(int, int);

There are two problems with that:

1) It forces the user to write unsafe code even if he didn't want to (in
other words, even if in his case it would be enough to use a local,
stack-allocated object of type 'point'). When things are allocated
dynamically it's way too easy to make mistakes which cause memory leaks,
and this scheme *forces* the user to allocate things dynamically. (Not
that the problem wouldn't exist in C++ as well, but at least C++ offers
tools to avoid the problem, such as automatic destructor calls when
local objects go out of scope.)

2) malloc() is a slow operation in most systems, much slower than using
local stack-allocated objects. (We are talking about over 10 times
slower in the worst case.) This scheme forces the user to allocate the
objects dynamically even if there was no need.
Also, having to access the members of the object through functions
which cannot be inlined by the compiler (as this scheme is designed
precisely for that purpose) makes using those objects much slower.

Safety Articles | Usenet Groups | Usenet News | Bluegrass