Re: CFO: Why C?

From: VBDis (vbdis_at_aol.com)
Date: 09/10/03


Date: 09 Sep 2003 23:37:31 GMT

Im Artikel <yw1xbrtvr69t.fsf@users.sourceforge.net>, mru@users.sourceforge.net
(=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=) schreibt:

>It is often said that Java has no pointers. This is easily proven
>wrong. Just look up NullPointerException. Renaming pointers to
>references doesn't change anything fundamental.

The difference between pointers and references is more than only a rename. At
least in C++ references cannot be NULL, that's a significant difference from
unmanaged pointers. References also cannot be modified, by pointer arithmetic.
What remains is a check for possibly uninitialized variables.

>It's true that if pointers are not used, certain errors can't happen.
>However, it's not only errors that are banished by this. A lot of
>nice functionality is lost as well.

Can you give concrete examples?

>Doing things perfectly safely is impossible without runtime checks.

Agreed, when dynamic arrays come into play. Do you know of other situations?

>Runtime checks always take some extra time.

Sometimes appropriate runtime checks can even save time! Consider a loop over
an array, where in traditional code often a pointer as well as an index (loop
variable) is incremented or decremented and tested for every iteration. With
indexed access to the array the compiler will almost use the same procedure,
with an implicit pointer, but it can create faster code for the loop test
(LOOPNE or similar, for the assembly freaks ;-)

In another case, when counted instead of zero terminated strings are used, a
test for the trailing null byte is obsolete, since the length of the string is
known before entering the loop, and can immediatley be used as the loop count.
Similarly NULL pointers deserve no checking code at all, when references are
used.

>Even with the checks,
>there is nothing you can do but abort, when there is a violation.

Compile time checks reduce the amount of remaining runtime checks. I just can
imagine only two possible cases, but perhaps you find more:
- index bounds violation
- out of memory during allocation

In the first case it should be possible to find the place (loop...) of the
erroneous index calculation. The second case usually indicates an infinite
loop, which also should be locatable in the code.

>IMHO, it is better to use a debugging tool, such as valgrind, to check
>the program during development.

There exist fine tools, which can reduce the time to find the remaning bugs in
some code. In the best case they allow to verify that no errors can occur at
all, provided the code is written in a way that allows for such a verification.

DoDi



Relevant Pages

  • Re: newbie question
    ... The second assigns a pointer to an array 1000 of char to an int pointer. ... What you pass are the file pointers, ... > second file is identical to the first. ... Look at your while loop. ...
    (comp.lang.c)
  • Re: loop performance question
    ... | such that every possible pair in the array is tested. ... | whenever I access any data in the second loop. ... The pointers to separately allocated objects break the ... Then comes the question of how to avoid cache misses when accessing obj2. ...
    (comp.lang.cpp)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • Re: list implementation
    ... >I believe the type "list" is implemented as an array of pointers. ... A Python list is sematically/behaviorally defined as a mutable extensible ... the references are arrays of C pointers. ...
    (comp.lang.python)
  • Re: =& when creating new object
    ... $reference point to the same memory location. ... Please read the chapter called "references are not pointers" in the ... should not consider it an address in PHP. ...
    (comp.lang.php)