I think your TLogData contains storage for an integer (TLogType, 4 bytes), and a pointer to an array (4 bytes).<br><br>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.<br>
<br><div class="gmail_quote">On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) <span dir="ltr"><<a href="mailto:delphi@moorhouse.net.nz">delphi@moorhouse.net.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I have the following code snippet<br>
<br>
<code><br>
type<br>
PConstArray = ^TConstArray;<br>
TConstArray = array of TVarRec;<br>
<br>
function CreateConstArray(const Elements: array of const): TConstArray;<br>
<br>
type<br>
TLogType = (ltError, ltWarn, ltInfo);<br>
PLogData = ^TLogData;<br>
TLogData = record<br>
LogType: TLogType;<br>
LogArgs: TConstArray;<br>
end;<br>
<br>
....<br>
<br>
procedure TUserClass.Log(const LogType: TLogType; const Args: array of<br>
const );<br>
var<br>
LogData: PLogData;<br>
begin<br>
// size of record TLogData does not work<br>
GetMem(LogData, sizeof(TLogData));<br>
LogData.LogType := LogType;<br>
// blows up on next line<br>
LogData.LogArgs := CreateConstArray(Args);<br>
// ... do some other stuff with the LogData item finally calling FreeMem<br>
end;<br>
<br>
function CreateConstArray(const Elements: array of const): TConstArray;<br>
var<br>
I: Integer;<br>
begin<br>
SetLength(Result, Length(Elements));<br>
for I := Low(Elements) to High(Elements) do<br>
Result[I] := // assign a TVarRec here<br>
end;<br>
</code><br>
<br>
The code that assigns the memory only assigns 8 bytes - and an access<br>
violation ensues. If I replace the call to "sizeof" with the number 16,<br>
the code works fine.<br>
<br>
My understanding of dynamic arrays was that the compiler created a 4 byte<br>
field before the first element that contained the length of the array.<br>
<br>
So why does the sizeof function not reflect this ? And why do I need 16<br>
bytes not 12 (4 for LogType + 4 for length of array + 4 for array<br>
pointer)?<br>
Also regardless of the number of items in the open array parameter, 16<br>
bytes works, so it does not relate the length of the TConstArray.<br>
<br>
Your thoughts ?<br>
<br>
David<br>
<br>
<br>
<br>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@delphi.org.nz">delphi-request@delphi.org.nz</a> with Subject: unsubscribe<br>
</blockquote></div><br>