Re: SetSize...



alanglloyd@xxxxxxx wrote:
Rob Kennedy wrote:
Ove wrote:
<snip>
type PDataRec = ^ADataRec;
ADataRec = record
<snip>
p := New(PDataRec);

Usually, that's just written "New(p);" not as an assignment statement.

But better written ..

var
PtrDataRec : PDataRec;

PtrDataRec := AllocMem(SizeOf(TDataRec);

I disagree. For one thing, you now have to make sure you get the size correct, whereas with New, the compiler figures out the size based on the type of the variable you're using.

... then not only is the memory zero-ed (each byte set to 0)

Which is only correct if the default initializations of all the record's fields is all-bits-zero, and that is not necessarily the case. The New procedure will initialize all the fields that need initialization as though the Initialize procedure had been called.

but the
pointer is properly typed (instead of being a generic pointer)

The p variable in Ove's and my code was not a generic pointer. If it were, my call to New would not have compiled.

and thus
it is brought within Delphi's strict type-checking, which protects you
from a lot of errors and results in a better standard of coding.

Not really. AllocMem returns type Pointer. You can assign that to any pointer-typed variable. (I think you can even assign it to an object reference or interface reference!) There's no checking to ensure that the value you get is assigned to a PDataRec value just because you called AllocMem with SizeOf(TDataRec).

Of course don't forget to ...

FreeMem(PtrDataRec) when you have finished with the record.

And that' the worst part. When you do that, you leak the dynamic array in the record. FreeMem doesn't know anything about the type of the variable you give to it. All it knows is the size of the memory block. The Dispose procedure, on the other hand, will behave as though Finalize were called on the variable before freeing the memory.

New = GetMem + Initialize
Dispose = Finalize + FreeMem

--
Rob
.



Relevant Pages

  • Re: bug in CEPC boot loader!!!!
    ... Michel Verhagen (eMVP) wrote: ... and DOS needs a BIOS. ... without problems, or just initialize the most important devices (SDRAM, ... BIOSLOADER or LoadCEPC that is initializing the pointer at the location ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Multiple indirection mess-up...
    ... >In the following program I allocate a block of pointers to type char, ... I suspect the fault is in the pointer manipulations between ... calloc sets each byte to all bits zero. ... Since you do properly initialize each pointer ...
    (comp.lang.c)
  • Re: Question about arrays of derived types and POINTERs
    ... initialize surfinstead of allocating a temporary surfptr and then ... a pointer starts "life" with an undefined association ... nulifying the x, y, and z pointers in each element when you initialize ...
    (comp.lang.fortran)
  • Re: WRITETEXT - Im utterly confused By BOL
    ... No - a default constraint of NULL will not initialize the text pointer. ... MyId int NOT NULL ... MyTextData text NULL DEFAULT NULL ...
    (microsoft.public.sqlserver.programming)