I often need to store a lot of strings, and don't want to waste the space allocated to a String(255) for each one. With "short" strings, I just allocate memory (with the Mac system NewPtr routine, not with the Pascal New routine) for Length(s)+1 bytes, and then BlockMove the string in. This has the advantage that the string can be references as just Str255Ptr(s)^ (sometimes the size needs to be aligned to 2 or 4 bytes, depending on the platform).
I could do the same thing with GPC's Schema String(255), but I'd have to pack them in, and then unpack them out to a temporary variable to access them, I could not access them in place in the packed data block.
I presume there are clever ways to do such things in GPC/EP? What would be the options for packing strings into a block of memory and yet still having them usable?
Thanks, Peter.
Peter N Lewis wrote:
I often need to store a lot of strings, and don't want to waste the space allocated to a String(255) for each one. With "short" strings, I just allocate memory (with the Mac system NewPtr routine, not with the Pascal New routine) for Length(s)+1 bytes, and then BlockMove the string in. This has the advantage that the string can be references as just Str255Ptr(s)^ (sometimes the size needs to be aligned to 2 or 4 bytes, depending on the platform).
I could do the same thing with GPC's Schema String(255), but I'd have to pack them in, and then unpack them out to a temporary variable to access them, I could not access them in place in the packed data block.
I presume there are clever ways to do such things in GPC/EP? What would be the options for packing strings into a block of memory and yet still having them usable?
var p: ^String;
New (p, ActualLength);
simply use p^
E.g.:
program Foo (Input, Output);
var StaticString: String (2048); { or whatever you like } p: ^String;
begin ReadLn (StaticString); New (p, Length (StaticString)); p^ := StaticString; WriteLn (p^) end.
No type-casts, hand-made size computations, worries about alignment or other dirty tricks necessary. That's one of the nice things about schemata (of which strings are a special case).
Frank
At 5:58 +0200 6/7/05, Frank Heckenbach wrote:
Peter N Lewis wrote:
I often need to store a lot of strings, and don't want to waste the space allocated to a String(255) for each one. With "short" strings, I just allocate memory (with the Mac system NewPtr routine, not with the Pascal New routine) for Length(s)+1 bytes, and then BlockMove the string in. This has the advantage that the string can be references as just Str255Ptr(s)^ (sometimes the size needs to be aligned to 2 or 4 bytes, depending on the platform).
I could do the same thing with GPC's Schema String(255), but I'd have to pack them in, and then unpack them out to a temporary variable to access them, I could not access them in place in the packed data block.
I presume there are clever ways to do such things in GPC/EP? What would be the options for packing strings into a block of memory and yet still having them usable?
var p: ^String;
New (p, ActualLength);
I'm always hesitant to use Pascal's New in case it does weird things like many Pascals do to support Mark/Release/etc. It appears that GPC calls straight through to malloc/free, with little overhead and no messing around unless Mark/Release procedures are called.
No type-casts, hand-made size computations, worries about alignment or other dirty tricks necessary. That's one of the nice things about schemata (of which strings are a special case).
Indeed, that does look to be a nice solution - I'm looking forward to using all the GPC/EP goodness once I escape from CW, but first I have to get there! 20 more units to go until the fun begins. Peter.
Peter N Lewis wrote:
At 5:58 +0200 6/7/05, Frank Heckenbach wrote:
var p: ^String;
New (p, ActualLength);
I'm always hesitant to use Pascal's New in case it does weird things like many Pascals do to support Mark/Release/etc. It appears that GPC calls straight through to malloc/free, with little overhead and no messing around unless Mark/Release procedures are called.
Yes. (And quite frankly, Pascal without `New' would be quite a crippled language. For Mac Objects, besides many other things, you need `New' anyway, don't you?)
Frank
At 7:17 +0200 6/7/05, Frank Heckenbach wrote:
Peter N Lewis wrote:
I'm always hesitant to use Pascal's New in case it does weird things like many Pascals do to support Mark/Release/etc. It appears that GPC calls straight through to malloc/free, with little overhead and no messing around unless Mark/Release procedures are called.
Yes. (And quite frankly, Pascal without `New' would be quite a crippled language. For Mac Objects, besides many other things, you need `New' anyway, don't you?)
For Mac objects, there is no choice.
For all other pointers, I use the system call NewPtr, or NewHandle (a Mac Handle is a Ptr to a Ptr that the memory manage knows as a special construct allowing it to be resized while the handle remains unchanged, it was used a lot in the earlier days of much more restricted memory to allow the memory manager to defragment the heap).
For serious programming, some Pascal's horrendous memory allocation schemes render the language next to useless. Several Pascals have their own memory allocation system so that once memory is taken from the system it is never returned.
But I'm happy to hear I can use it safely with GPC and it will just call through to the system malloc/free.
CW Pascal is probably the same, but old habits (and anxieties) die hard. Peter.