Ignoring values (was: --delphi bug?)

Frank Heckenbach ih8mj at fjf.gnu.de
Wed Dec 31 15:07:08 CET 2003


CBFalconer wrote:

> This can lead to unexpected and baffling errors, especially in C.
> My classic example is:
> 
> char * revstring(char *s)
> {
>    /* whatever, to reverse string in place, and return s */
> }
> 
> ....
> 
>    char thestring[] = "whatever";
> 
>    printf("\"%s\" \"%s\"\n", thestring, revstring(thestring));
> 
> which should print out:
> 
>    "revetahw" "revetahw" 
> 
> contrary to expectations.  The cure is to make revstring a void
> function, or a procedure in Pascal.  Moral: discarding function
> results should be awkward.

That's not primarily a problem of ignoring function results, but of
(a) the existence of functions with side-effects and (b) mistaking
semantics (in particular, thinking of C strings as string values
rather than pointers).

(a) is inherent in Pascal as well. We could discuss whether
functions should have been defined as having no side-effects, but
this would be an academic discussion since we won't change the
basics of Pascal.

(b) is true whenever using C strings (and in some other situations).
That's why I generally recommend to avoid them. But if you use them,
you'll just have to remember to look at semantics more carefully
(which not every C programmer does which leads to strange errors,
sure ...).

Frank

-- 
Frank Heckenbach, frank at g-n-u.de, http://fjf.gnu.de/, 7977168E
GPC To-Do list, latest features, fixed bugs:
http://www.gnu-pascal.de/todo.html
GPC download signing key: 51FF C1F0 1A77 C6C2 4482  4DDC 117A 9773 7F88 1707




More information about the Gpc mailing list