Just hard code 16 in David and we can "discuss" it we we do a code review :-)<br><br><div class="gmail_quote">On Thu, Aug 25, 2011 at 12:36 PM, <span dir="ltr"><<a href="mailto:kaller@ihug.co.nz">kaller@ihug.co.nz</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><p>sure ok.</p><div class="im">
<p> </p>
<p> </p>
<p> </p>
<p>On Thu, 25 Aug 2011 11:57:26 +1200, Robo <<a href="mailto:robo555@gmail.com" target="_blank">robo555@gmail.com</a>> wrote:</p>
</div><blockquote style="border-left:#1010ff 2px solid;padding-left:5px;width:100%;margin-left:5px"><div class="im">
<p>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.</p>
</div><div class="gmail_quote"><div class="im">On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) <span><<a href="mailto:delphi@moorhouse.net.nz" target="_blank">delphi@moorhouse.net.nz</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">I have the following code snippet<br><br></div><code><br><div><div></div><div class="h5">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></div></div></code><br><br><div class="im">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" target="_blank">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" target="_blank">delphi-request@delphi.org.nz</a> with Subject: unsubscribe<br>
</div></blockquote>
</div>
<br></blockquote>
<p> </p>
<p> </p><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>