mcdonald@fornax.UUCP (Ken Mcdonald) (02/06/89)
Here's a simple question I'd appreciate some advice on. I'm using LightSpeed Pascal 2.0 (great product, by the way, Rich), and I want to dynamically allocate arrays of structured types, with the number of elements in the arraynot known until the array is actually allocated. The thing is, while I don't mind messing around with doing funny type conversions and pointer variables and things like that while I'm creating the array, I don't want to have to do things like that when I'm actually using it. I don't mind it if it's a hassle to create, but I don't want that kind of hassle to use it. So for example, I want to be able to do: program main; type complex=record re: real; im: real end cptr=^array[0..0] of complex; var C: cptr begin {Whatever is necessary here to create an array big enough to hold,say} {16 complexes, and set C to point to it.} C^[1].re := 1 C^[1].im := 1 ... end. Yes, I know that I'm missing lots of semicolons and where to put them, and so do you, now if only the compiler knew as well. Anyway, is there a way of doing something similar to the above and having it work? Do I have to adjust my subscripts? Are there better ways of doing the same thing? And how would I achieve the same effect, but with handles (relocateable blocks) instead of pointers (nonrelocateable blocks.) Please don't suggest linked structures, the Memory Manager imposes too much overhead on blocks for me to want to do this. If there is an easy answer in Inside Mac or my LSP manual, just point me to it, no need to wear your fingers out typing. Thanks, Ken McDonald {...!ubc-cs!mcdonald@fornax.uucp} P.S. Mail to my address isn't very reliable, so it might be better to post an answer, rather than mailing it. I think the question is of reasonably general interest (although perhaps kinda simple) to justify this.
lwvanels@athena.mit.edu (Lucien Van Elsen) (02/07/89)
In article <867@fornax.UUCP> you write: >Here's a simple question I'd appreciate some advice on. I'm using LightSpeed >Pascal 2.0 (great product, by the way, Rich), and I want to dynamically >allocate arrays of structured types, with the number of elements in the >arraynot known until the array is actually allocated. I solved a similar problem by declaring a type that's an array as large as you might ever possiblly want it, and then creating a handle in the program that's only as large as you need. I.E. type comparray : array[1..maxnum] of complextype; compptr : ^comparray; comphan : ^compptr; myhandle := comphan(Newhandle(sizeof(wanted*sizeof(complextype)))); (where wanted is the desired number of elements in the array) You have to take care that you never reference an index larger than the number you have allocated- there's nothing to stop you from going past the end of the handle you have allocated. However, it is possible to dynamically grow/shrink the array while the program is running using SetHandleSize(myhandle,newnum*sizeof(complextype)). Hope this helps! Lucien Van Elsen Internet:lwvanels@athena.mit.edu or lve@wheaties.ai.mit.edu UUCP: ...!mit-eddie!mit-athena!lwvanels
jac@petsd.UUCP (Jim Clausing) (02/08/89)
In article <9127@bloom-beacon.MIT.EDU> lwvanels@athena.mit.edu (Lucien Van Elsen) writes: [...stuff deleted...] >myhandle := comphan(Newhandle(sizeof(wanted*sizeof(complextype)))); Is this right? ---------------^^^^^^ I don't often call NewHandle, but shouldn't it be NewHandle(wanted*sizeof(..))? Otherwise, thanx for the tip. [...more stuff deleted...] >Lucien Van Elsen -- Jim Clausing Voice: (201)758-7693 Parallel Processing Tools Grp. CI$: 74726,2015 GEnie: J.CLAUSING Concurrent Computer Corp. Internet: jac@petsd.ccur.com Tinton Falls, NJ 07724 UUCP: {att, rutgers, princeton}!petsd!jac