TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) (05/10/91)
>>> >>> type byte_buf = array[1..64000] of byte; >>> byte_buf_ptr = ^byte_buf; >>> >>> var buffer : pointer; >>> . >>> . >>> getmem(buffer,somesize); { Allocate only as many bytes as you need } >>> byte_buf_ptr(buffer)^[2] := somevalue; >> >>Actually, since GETMEM takes ANY pointer type, you don't need >>the typecast. You don't have to allocate enough memory for the >>structure you're loading; you just have to be sure your code >>will manage dynamic structures correctly. >> > > You most certainly do need the typecast. Notice the getmem call > does not use the typecast; it's the reference to the pointer > as a pointer to an array of bytes that requires the typecast. > Guess you weren't reading too close :). Actually, GetMem works fine with or without a type cast. Both are right. I think what should have been said is that Type cast ARE needed in the above example, but are not required in turbo pascal with the GetMem procedure. The following code works fine WITHOUT a type cast: TYPE BufType = ARRAY [1..1] OF BYTE ; VAR PBuf : ^BufType ; BEGIN GetMem ( PBuf , 5000 ) ; { Allocate 5000 bytes at PBuf^ } PBuf^[4000]:= 200 ; FreeMem ( PBuff , 5000 ) ; END . tom +--------------------------------------------------------------------------+ | Thomas E. Jenkins, Jr. Programmer, University of South Carolina CSD | +--------------------------------------------------------------------------+ | BITNET : C0361@UNIVSCVM.BITNET | CSDNET : tomj/csdserver3 | | INTERNET : TOMJ@csdserver3.csd.scarolina.EDU {PREFERRED} | | : C0361@univscvm.csd.scarolina.EDU | 129.252.43.30 | | FROM Compuserv : INTERNET:TOMJ@csdserver3.csd.scarolina.EDU {PREFERRED} | | : INTERNET:C0361@univscvm.csd.scarolina.EDU | +--------------------------------------------------------------------------+
bobb@vice.ICO.TEK.COM (Bob Beauchaine) (05/10/91)
In article <26835@adm.brl.mil> TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) writes: > > GetMem works fine with or without a type cast. Both are right. I think >what should have been said is that Type cast ARE needed in the above >example, but are not required in turbo pascal with the GetMem procedure. >The following code works fine WITHOUT a type cast: > [code deleted] In defense of my original posting which started this debate, there is one very good reason for using type casts the way I did. I have precompiled (in unit form) graphing routines that don't know what kind of data to expect at run time. I allocate the memory with an untyped pointer, then use a typecast on the pointer to the array when the graphics are invoked according to an enumerated type passed into the procedure. This allows me to use all of the integer and IEEE real forms as array elements. Hence the solution that naturally came to mind. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Bob Beauchaine bobb@vice.ICO.TEK.COM Heaven, n.: A place where the wicked cease from troubling you with talk of their own personal affairs, and the good listen with attention while you expound your own. Ambrose Bierce, "The Devil's Dictionary"
marcos@outland.UUCP (Marcos Della) (05/13/91)
TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) writes: > >>> > >>> type byte_buf = array[1..64000] of byte; > >>> byte_buf_ptr = ^byte_buf; > >>> > >>> var buffer : pointer; > >>> . > >>> . > >>> getmem(buffer,somesize); { Allocate only as many bytes as you need } > >>> byte_buf_ptr(buffer)^[2] := somevalue; > >> > >>Actually, since GETMEM takes ANY pointer type, you don't need > >>the typecast. You don't have to allocate enough memory for the > >>structure you're loading; you just have to be sure your code > >>will manage dynamic structures correctly. > >> > > > > You most certainly do need the typecast. Notice the getmem call > > does not use the typecast; it's the reference to the pointer > > as a pointer to an array of bytes that requires the typecast. > > Guess you weren't reading too close :). > > Actually, > > GetMem works fine with or without a type cast. Both are right. I think > what should have been said is that Type cast ARE needed in the above > example, but are not required in turbo pascal with the GetMem procedure. > The following code works fine WITHOUT a type cast: > > TYPE > BufType = ARRAY [1..1] OF BYTE ; > VAR > PBuf : ^BufType ; > BEGIN > GetMem ( PBuf , 5000 ) ; { Allocate 5000 bytes at PBuf^ } > PBuf^[4000]:= 200 ; > FreeMem ( PBuff , 5000 ) ; > END . > Something neat that can be done with the GETMEM procedure is an implementation of the new NEWSTR() in TV under TP6. This is how we used to do it. TYPE string_ptr = ^STRING; VAR entry : ARRAY[1..50] OF string_ptr; BEGIN ... GETMEM(entry[x],LENGTH(st) + 1); entry[x]^ := st; ... {This dynamically assigns string space as needed. Later...} ... FREEMEM(entry[x],LENGTH(entry[x]^) + 1); {This releases the string entry. Kinda like DISPOSESTR()} END; For those that don't use the TVision package, this is a good way to save a little heap space when you have arrays of strings that don't change quite a bit. Remember that if you DO change the string, you need to FREEMEM() it before GETMEM() to do a re-assignment. IF you do this often enough, you start fragmenting your heap like crazy! Thats why you want to use a relatively static system of strings. You can SWAP strings rather easily just by swapping the pointers (for instance, in a swap routine!) The best is that this is not on the data stack (other than the array I created of pointers) and you SAVE memory! Just remember the drawbacks Marcos ____________________________________________________________________________ Marcos R. Della | Did you ever wonder if there was outland!marcos@petunia.CalPoly.EDU (home) | more to the universe than this? mdella@polyslo.CalPoly.EDU (school) | marcos.della@f185.n125.z1.fidonet.org | ...I didn't...