Re: CFO: Why C?

From: Måns Rullgård (mru_at_users.sourceforge.net)
Date: 09/10/03


Date: Wed, 10 Sep 2003 11:15:46 +0200

vbdis@aol.com (VBDis) writes:

>>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.

I explicitly mentioned Java references. C++ references are something
quite different.

>>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?

Pointer arithmetic comes to mind. My old example of extracting an
integer from a byte array is another example.

>>Doing things perfectly safely is impossible without runtime checks.
>
> Agreed, when dynamic arrays come into play. Do you know of other situations?

Any use of dynamically allocated memory (not only arrays), or indexing
static arrays with a variable index could result in illegal accesses.

>>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 ;-)

Well, that ain't the way I code. Even if I did, the compiler would
most likely eliminate one of the variables. Have you ever looked at
the output of an optimizing C compiler.

> 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.

What about the common use of NULL pointers to signify an error, or
default value?

>>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

Use of uninitialized memory. This is more or less impossible to do
efficiently, and totally impossible at compile time. The solution is
a debugger, like valgrind. Memory leaks are also difficult find
without some overhead. Don't say garbage collector. I don't like
them.

-- 
Måns Rullgård
mru@users.sf.net


Relevant Pages

  • Re: code optimiation
    ... Given that the compiler can often optimise the generated code to use the best sized types available, it's seldom worth specifying "fast" types explicitly. ... pointers and floating point types whose "zero value" might not be all- ... instruction, so the assembler produced for *p++ when used as the ... It will do the same job, and let you write the source code using proper array constructs. ...
    (comp.arch.embedded)
  • Re: Q: Checking the size of a non-allocated array?
    ... an actual argument is already invalid ... First note that you don't have an unallocated array in the subroutine. ... it is comparable to disassociated or undefined pointers. ... Obviously the compiler has ...
    (comp.lang.fortran)
  • Re: How to retrieve data from array of pointers or from a struct?
    ... You're declaring an array of pointers to unsigned long long, ... you're initializing the pointers with integer values. ... and your compiler should have warned ... You're not explicitly calling memcpy, ...
    (comp.lang.c)
  • 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)