Re: c / c++ : is it end of era ?



Richard Heathfield a écrit :
jacob navia said:

<snip>

it is not
very difficult to find problems with the approach in C to many things,
since the bugs in the language aren't that difficult to find.


Do you have at least two examples that will stand up to close scrutiny?


Well, I hope we can start a constructive discussion, instead of
flame wars.

The most glaring bugs in C are:

1) Zero terminated strings. This is the source of countless problems,
because each access to a string implies an unbounded search for
the terminating zero, and becasue size information is not stored
explicitely in the string but must be reconstructed, so that
buffer overflows when copying those strings are almost inevitable.

Bounded strings can be written in C like this:

typedef struct tagString {
size_t length;
char *data;
unsigned flags;
} String;

Those are resizable strings. Non-resizable can be described
like this
typedef struct tagFixedString {
size_t length;
int flags;
char data[];
} FixedString;

I give this definitions to block people that say that
using other types of strings is impossible in C.
In the lcc-win32 compiler system, those strings are supported
in a special library.

2) Confusion between pointers and arrays. Arrays in C are completely
screwed up. There is endless confusion between pointers and
arrays specially because the size information is destroyed across
function calls.

3) From (1) and (2) we obtain as a consequence the inherent
impossibility to make bounds checks when accessing arrays and
strings. This leads to endless bugs.


The fix is proposed in lcc-win32: a few improvements to the language and
we can get rid of zero terminated strings and arrays as pointers.

Another big problem is the error-prone malloc/free combination. We have
discussed this here several times. The solution is to use an automatic
software component (garbage collector) that manages the release of the
allocated memory. Lcc-win32 proposes this in its standard distribution.

Note that the objective here is not just to say what is wrong but to
propose solutions. That is why I mention lcc-win32, that is free anyway,
so I have no financial gain from it.

jacob
.



Relevant Pages

  • Re: Can C do it ?
    ... Strings and arrays *are* pointers in C. ... If you intend to use strings and arrays, you must use pointers. ... /* this typedef makes mot into a type which can hold ...
    (comp.lang.c)
  • Re: Whats wrong with strcpy/strcat in this "C for Tcl" experiment?
    ... EL> feeling") for pointers leading into invalid areas. ... EL> less specific quirks regarding notation and its semantics to be learned ... Pascal handles 'strings' differently. ... Just pointers to characters and arrays of characters. ...
    (comp.lang.tcl)
  • Re: I need some basic C++ help
    ... I think the idea of using arrays for strings (one that comes entirely ... the students need to see how strings can be constructed using arrays ... > One reason is that they spend much less time debugging. ...
    (comp.lang.cpp)
  • Re: more idle thinking: SiMPL
    ... int *pi; ... memory for strings and arrays are dynamicly allocated, ...
    (comp.lang.misc)
  • Re: Whats wrong with strcpy/strcat in this "C for Tcl" experiment?
    ... > are more like Tcl's strings than C. ... Just pointers to characters and arrays of characters. ...
    (comp.lang.tcl)