[DUG] Sizeof record gives error
Jeremy Coulter
jscoulter at gmail.com
Thu Aug 25 14:25:44 NZST 2011
Just hard code 16 in David and we can "discuss" it we we do a code review
:-)
On Thu, Aug 25, 2011 at 12:36 PM, <kaller at ihug.co.nz> wrote:
> sure ok.
>
>
>
>
>
>
>
> On Thu, 25 Aug 2011 11:57:26 +1200, Robo <robo555 at gmail.com> wrote:
>
> I think your TLogData contains storage for an integer (TLogType, 4 bytes),
> and a pointer to an array (4 bytes).
>
> However, what you need is enough memory to store the entire array, not just
> the pointer, so you should get the size of the array to get the correct
> size.
> On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) <
> delphi at moorhouse.net.nz> wrote:
>
>> I have the following code snippet
>>
>>
>> type
>> PConstArray = ^TConstArray;
>> TConstArray = array of TVarRec;
>>
>> function CreateConstArray(const Elements: array of const): TConstArray;
>>
>> type
>> TLogType = (ltError, ltWarn, ltInfo);
>> PLogData = ^TLogData;
>> TLogData = record
>> LogType: TLogType;
>> LogArgs: TConstArray;
>> end;
>>
>> ....
>>
>> procedure TUserClass.Log(const LogType: TLogType; const Args: array of
>> const );
>> var
>> LogData: PLogData;
>> begin
>> // size of record TLogData does not work
>> GetMem(LogData, sizeof(TLogData));
>> LogData.LogType := LogType;
>> // blows up on next line
>> LogData.LogArgs := CreateConstArray(Args);
>> // ... do some other stuff with the LogData item finally calling FreeMem
>> end;
>>
>> function CreateConstArray(const Elements: array of const): TConstArray;
>> var
>> I: Integer;
>> begin
>> SetLength(Result, Length(Elements));
>> for I := Low(Elements) to High(Elements) do
>> Result[I] := // assign a TVarRec here
>> end;
>>
>>
>> The code that assigns the memory only assigns 8 bytes - and an access
>> violation ensues. If I replace the call to "sizeof" with the number 16,
>> the code works fine.
>>
>> My understanding of dynamic arrays was that the compiler created a 4 byte
>> field before the first element that contained the length of the array.
>>
>> So why does the sizeof function not reflect this ? And why do I need 16
>> bytes not 12 (4 for LogType + 4 for length of array + 4 for array
>> pointer)?
>> Also regardless of the number of items in the open array parameter, 16
>> bytes works, so it does not relate the length of the TConstArray.
>>
>> Your thoughts ?
>>
>> David
>>
>>
>>
>> _______________________________________________
>> NZ Borland Developers Group - Delphi mailing list
>> Post: delphi at delphi.org.nz
>> Admin: http://delphi.org.nz/mailman/listinfo/delphi
>> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
>> unsubscribe
>>
>
>
>
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20110825/dfe0b31e/attachment.html
More information about the Delphi
mailing list