Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!olivea!uunet!zephyr.ens.tek.com!uw-beaver!rice!hsdndev!cmcl2!adm!news From: TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) Newsgroups: comp.lang.pascal Subject: Type cast? Message-ID: <26835@adm.brl.mil> Date: 9 May 91 17:05:55 GMT Sender: news@adm.brl.mil Lines: 57 >>> >>> 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 | +--------------------------------------------------------------------------+