Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mit-eddie.UUCP Path: utzoo!watmath!clyde!cbosgd!ihnp4!ucbvax!decvax!genrad!mit-eddie!jbs From: jbs@mit-eddie.UUCP (Jeff Siegal) Newsgroups: net.cse Subject: Pascal Dynamic Strings Message-ID: <5388@mit-eddie.UUCP> Date: Thu, 26-Sep-85 10:27:32 EDT Article-I.D.: mit-eddi.5388 Posted: Thu Sep 26 10:27:32 1985 Date-Received: Sun, 29-Sep-85 05:12:51 EDT References: <433@uvm-cs.UUCP> <1500@brl-tgr.ARPA> <647@bu-cs.UUCP> <150@l5.uucp> <685@bu-cs.UUCP> Reply-To: jbs@mit-eddie.UUCP (Jeff Siegal) Organization: MIT, Cambridge, MA Lines: 76 >In Pascal the length of an array is part of it's type. Strings are >generally stored in 'packed array of char' (or array of char, no >difference for this point.) Now, consider the strlen function in >Pascal: > >constant > maxstring = 256 ; >type > string: packed array [1..maxstring] of char ; >var > buf : string ; > > function strlen(str : string) : integer ; > begin > strlen = maxstring > end ; { strlen } > >Kinda stupid, huh!? But you just can't pass different length arrays >to a Pascal routine, pointers don't help (as a C programmer might have >guessed) as they are typed according to the object they point to and >you just end up back to where you started. There are no casts or some >such to help. > Not exactly. The ISO Pascal standard allows for "conformant parameters in which a procedure (or function) can be declared as such: function strlen(str:packed array[1..n] of char):integer; begin strlen := n; end; writeln(strlen("Hello There"):1); Perhaps your compiler doesn't supprot this... But that's a different story :-) > 2. You could use some other representation of string, when I > brought this up with a faculty member her solution was > 'you should just represent strings as linked lists of chars' > [no comment] > A better (i.e. usable :-)) solution is to represent strings a linked lists of character "chunks" perhaps 16 characters. This allows both dynamic length strings, and some measure of efficiency. I would perfer though to combine this meathod with another: type blocks: record chars: packed array[1..blocksize] of char; rest: ^blocks end; string: record len:0..maxint; body:^blocks end; > 3. Some Pascals have strings built in as a 'natural' data type: > No, all Pascals do this: "Hello" is defined to be a packed array[1..5] of char. But I suppose this doesn't help the problem of dynamic strings. > > b) More importantly, this is just an example, solving the > special case of strings still doesn't let me write > C's qsort() routine (try it, it *cannot* be written in > Pascal in any useful way...sad, huh, ya gotta write a > different sort routine for every data type, array size > you use!) > see "conformant arrays" above. >The result: Everything ends up to be ad hoc one-shots, forget building >generic libraries, write your own everything. And completely forget >portability, it doesn't exist in the pascal world. > Jeff Siegal - MIT EECS