Related articles |
---|
[8 earlier articles] |
Re: Pascal vs C style string ? monnier@di.epfl.ch (Stefan Monnier) (1994-06-28) |
Re: Pascal vs C style string ? eru@tele.nokia.fi (Erkki Ruohtula) (1994-06-28) |
Re: Pascal vs C style string ? andrew@cee.hw.ac.uk (1994-06-28) |
Re: Pascal vs C style string ? jhallen@world.std.com (1994-06-28) |
Re: Pascal vs C style string ? larryr@pa.dec.com) (1994-06-28) |
Re: Pascal vs C style string ? boehm@parc.xerox.com (1994-06-28) |
Re: Pascal vs C style string ? cjmchale@dsg.cs.tcd.ie (1994-06-29) |
Re: Pascal vs C style string ? nandu@cs.clemson.edu (1994-06-29) |
Re: Pascal vs C style string ? Theo.Norvell@comlab.oxford.ac.uk (1994-06-30) |
Re: Pascal vs C style string ? guerin@IRO.UMontreal.CA (1994-06-30) |
Re: Pascal vs C style string ? synaptx!thymus!daveg@uunet.uu.net (Dave Gillespie) (1994-06-30) |
Re: Pascal vs C style string ? nickh@harlequin.co.uk (1994-07-01) |
Re: Pascal vs C style string ? mps@dent.uchicago.edu (1994-07-05) |
Newsgroups: | comp.compilers |
From: | cjmchale@dsg.cs.tcd.ie (Ciaran McHale) |
Keywords: | C, Pascal, design |
Organization: | DSG, Dept. of Computer Science, Trinity College Dublin |
References: | 94-06-214 94-06-224 |
Date: | Wed, 29 Jun 1994 11:26:03 GMT |
Erkki Ruohtula <eru@tele.nokia.fi> writes:
>I have too often seen careless C code like
>
>for (i = 0; i < strlen(very_long_string); ++i) {
> do_something_with(very_long_string[i]);
>}
>
>For those who do not know C, this loop unnecessarily scans through
>the whole very_long_string at every iteration. It would be quite OK
>if the length-prefixed representation were used.
This is an example of "you can write poor code in any language". I think
that the careless C code you have seen is the fault of the programmers
rather than the language since any self-respecting C programmer should
know how to rewrite that loop to remove the ineffeciency you mention.
Anecedote:
I used the p2c Pascal-to-C translator to help me port the code of a
compiler from Turbo Pascal (on a PC) to C so that I could use it on a UNIX
machine. _Overall_ I was happy with the generated C code but there were
some things that I was unimpressed with. One was the way in which it
translated Pascal string-manipulation into C string-manipulation. A
Pascal statement such as:
StringVar := StringVar + 'c'; { add a char to the end of the string }
was translated into C code like the following:
sprintf(StringVar, "%s%c", StringVar, 'c');
The problem here is a mismatch between how the two languages treat
strings. Since Paslcal strings are length-prefixed, Pascal programmers are
encouraged to manipulate strings in a particular manner that is efficient
for that type of string representation. However, this form of string
manipulation is quite inefficient when used on null-terminated strings as
in C. Thus a statement-to-statement source translator generates
inefficient code where string manipulation is concerned.
In case anyone is wondering in which part of the compiler this string
manipulation code appeared... It was in the lexical analyser. Appending
characters to the end of a string was used to construct the spelling of
identifiers and numbers. The UNIX/C port of that compiler probably had one
of the slowest lexical analysers ever seen in a compiler. I could have
rewritten that part of the generated code so that it manipulated strings
in a more efficient manner. However, since it was a research tool, speed
wasn't important and so I did not bother.
Ciaran.
--
Ciaran McHale (cjmchale@dsg.cs.tcd.ie)
Dist. Systems Group, Department of Computer Science, Trinity College,
Dublin 2, Ireland. Telephone: +353-1-7021539 FAX: +353-1-6772204
http://www.dsg.cs.tcd.ie:/dsg_people/cjmchale/cjmchale.html
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.