<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    You're both right.&nbsp; A call to GetMem followed by FillChar with #0
    sorts the problem :(<br>
    <br>
    However I will stick with New as it is a cleaner solution than all
    others mentioned :)<br>
    <br>
    D<br>
    <br>
    On 30/08/11 12:51, David Brennan wrote:
    <blockquote cite="mid:009201cc66ae$f6a59090$e3f0b1b0$@co.nz"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <meta name="Generator" content="Microsoft Word 12 (filtered
        medium)">
      <style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
      <div class="WordSection1">
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">I
            think the sizeof call is irrelevant here (which I believe is
            what Jolyon is also saying).<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">The
            error occurs on this line:<o:p></o:p></span></p>
        <p class="MsoNormal">LogData.LogArgs := CreateConstArray(Args);<span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">LogArgs
            is a dynamic array as is the CreateConstArray result.
            Standard Delphi logic is that the memory for the old dynamic
            array (ie LogArgs) will be dereferenced and then freed if no
            longer used. If LogArgs points to random memory because it
            was never initialised then obviously the dereferencing and
            subsequent freeing are both extremely likely to blow up.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">The
            compiler usually works pretty hard to make sure dynamic
            array variables are initialised to nil but the GetMem call
            was bypassing that.<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">Cheers,<o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D">David.
            <o:p></o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <p class="MsoNormal"><span
style="font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1F497D"><o:p>&nbsp;</o:p></span></p>
        <div style="border:none;border-top:solid #B5C4DF
          1.0pt;padding:3.0pt 0cm 0cm 0cm">
          <p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"
                lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"
              lang="EN-US"> <a class="moz-txt-link-abbreviated" href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a>
              [<a class="moz-txt-link-freetext" href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>] <b>On
                Behalf Of </b>Jolyon Smith<br>
              <b>Sent:</b> Tuesday, 30 August 2011 12:04 p.m.<br>
              <b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:rohit@cfl.co.nz">rohit@cfl.co.nz</a>; NZ Borland Developers Group -
              Delphi List<br>
              <b>Subject:</b> Re: [DUG] [computing] Sizeof record gives
              error<o:p></o:p></span></p>
        </div>
        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
        <p class="MsoNormal" style="margin-bottom:12.0pt">No, it is not.
          &nbsp;The compiler is perfectly correct.<br>
          <br>
          A "dynamic array" is a reference type. &nbsp;It is a POINTER to a
          structure that describes a dynamically sized and reference
          counted array, just as a "String" is a pointer to a structure
          that describes (in even more detail) a dynamically sized,
          reference counted String. &nbsp;In fact, an "array of Char", plus
          twiddly bits. &nbsp;:)<o:p></o:p></p>
        <div>
          <p class="MsoNormal">sizeof(TObject) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 4<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">sizeof(Pointer) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 4<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">sizeof(String) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 4<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal" style="margin-bottom:12.0pt">sizeof(dynamic
            array) = 4<br>
            <br>
            Is correct and true because all four types are _pointers_
            and (on Win32) a pointer is 32-bits == 4 bytes.<br>
            <br>
            The size of/amount of memory that each variable of those
            types points _to_ is different, and includes not only the
            "user data" (at the positive offsets from the address that
            the pointer references) but also RTL information at
            *negative* offsets. &nbsp;But "sizeof" is not concerned with (is
            in fact entirely ignorant of) this data, only the size of
            the type used to hold the *reference* to that data.<br>
            <br>
            <o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">type<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; &nbsp;tfoo = record<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; &nbsp; &nbsp;bar: array of integer;<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; &nbsp;end;<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
        </div>
        <div>
          <p class="MsoNormal">var<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; f: TFoo; &nbsp; &nbsp;// &lt;&lt; has a single
            field holding a *reference* to an empty array of integer;<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">begin<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; // sizeof(f) == 4 at this point - a
            pointer<o:p></o:p></p>
        </div>
        <div>
          <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
        </div>
        <div>
          <p class="MsoNormal">&nbsp; SetLength(f.bar, 20); // makes "bar"
            big enough for 20 integers - 80 bytes<o:p></o:p></p>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal">&nbsp; // sizeof(f) is STILL *4* at this
              point because "bar" is still just a pointer. &nbsp;<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal">&nbsp; // &nbsp;Furthermore, sizeof() is
              evaluated when you *compiled* the code so it cannot know&nbsp;<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal">&nbsp; // &nbsp;how much memory bar points to
              even if it wanted to.<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal">end;<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal" style="margin-bottom:12.0pt">It is the
              *initialization* of the memory *referenced* by your
              dynamic array variable that is crucial to the correct
              functioning of the "compiler magic" that supports such
              types. &nbsp;If you run this code in the debugger and place a
              breakpoint on the "SetLength", when the code stops on that
              breakpoint open the CPU view and look back and you will
              see that the compiler has injected a call to
              "InitializeRecord"... if you look in System.pas you will
              see that _InitializeRecord in turn calls _InitializeArray.<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal">It is this sequence of "Initialize"
              calls that is missing when you simply allocate a chunk of
              memory and then set a typed pointer to point at it without
              first doing all the housekeeping necessary to make that
              chunk of memory *behave* in the way that your typed
              pointer is going to expect it to.<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal">By all means do that simple memory
              allocation, but you are then responsible for doing the
              housekeeping... calling "Initialize".<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal">Or use "New" and let the RTL take care
              of any initialisation that may be required.<o:p></o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
          <div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
            <div>
              <p class="MsoNormal">On 30 August 2011 09:10, Rohit Gupta
                &lt;<a moz-do-not-send="true"
                  href="mailto:rohit@cfl.co.nz">rohit@cfl.co.nz</a>&gt;
                wrote:<o:p></o:p></p>
              <div>
                <p class="MsoNormal">Joylon,<br>
                  <br>
                  Good point.&nbsp; But I dont think it applies here.&nbsp; Sizeof
                  returns an incorrect result... Its a compiler flaw.<br>
                  <span style="color:#888888"><br>
                    Rohit</span><o:p></o:p></p>
                <div>
                  <div>
                    <p class="MsoNormal"><br>
                      <br>
                      <br>
                      <br>
                      On 29/08/2011 8:10 p.m., Jolyon Smith wrote: <o:p></o:p></p>
                    <div>
                      <div>
                        <p class="MsoNormal">Having just looked up the
                          references for New/GetMem it may not actually
                          be a question of amount of memory allocated
                          after all, but rather one of correct
                          initialization of the allocated memory.<o:p></o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal">GetMem() does not
                          initialize the allocated memory, New() does.<o:p></o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal">This is explained in the
                          documentation for the "Initialize()" procedure
                          (it might have been helpful if the
                          documentation for GetMem() had mentioned this
                          - or the lack there-of - too - LOL)<o:p></o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal">So you might find that
                          GetMem() would have worked had you also called
                          Initialise() on the pointer afterward (and
                          Finalize() before calling FreeMem()). &nbsp;But
                          whatever the underlying reasons it is
                          obviously simply much easier and safer (less
                          to remember to have to do, less to get wrong
                          in doing it :)) to use New/Dispose when
                          working with allocations of
                          variables/structured(typed) memory and use
                          GetMem/FreeMem when it is strictly speaking
                          *just* unstructured memory you need (e.g. i/o
                          buffers etc), rather than space for variables.<br>
                          <br>
                          Glad it helped with your problem in any event.
                          &nbsp;:)<o:p></o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                        <div>
                          <p class="MsoNormal">On 29 August 2011 17:40,
                            David Moorhouse &lt;<a
                              moz-do-not-send="true"
                              href="mailto:delphi@moorhouse.net.nz"
                              target="_blank">delphi@moorhouse.net.nz</a>&gt;
                            wrote:<o:p></o:p></p>
                          <div>
                            <p class="MsoNormal">I'm aware of the
                              compiler layout behind the scene - and the
                              fact that regardless of the length of the
                              dynamic array, my call to GetMem (or New)
                              does NOT have to allocate memory for the
                              dynamic array's contents, just it's
                              overhead :)<br>
                              <br>
                              However, the compiler gets the size right
                              using New rather than GetMem :) So thanks
                              for the tip.<br>
                              <br>
                              Cheers<br>
                              <span style="color:#888888"><br>
                                D</span> <o:p></o:p></p>
                            <div>
                              <div>
                                <p class="MsoNormal"><br>
                                  <br>
                                  <br>
                                  <br>
                                  <br>
                                  On 29/08/11 16:23, Jolyon Smith wrote:
                                  <o:p></o:p></p>
                                <p class="MsoNormal">Is it a compiler
                                  *error* or just a compiler _behaviour_
                                  ?<br>
                                  <br>
                                  I haven't looked into it in detail,
                                  but dynamic arrays are notoriously
                                  slippery when you are working with
                                  them at a low level and alarm bells
                                  started ringing as soon as I saw they
                                  were involved. <o:p></o:p></p>
                                <div>
                                  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal">In particular, a
                                    dynamic array is a reference type,
                                    like a string. &nbsp;So whilst their may
                                    be additional RTTI at a negative
                                    offset from the base address of the
                                    array, the "array" itself may well
                                    be a pointer, hence "sizeof()" will
                                    return 4 - the size of a pointer -
                                    no matter how many items may be in
                                    the array (as opposed to Length(),
                                    obviously).<br>
                                    <br>
                                    NOTE: &nbsp; sizeof(String) also yields
                                    "4" even though we all know that a
                                    String variable requires many more
                                    bytes than that.<o:p></o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><br>
                                    As far as this particular example
                                    goes, do you get any better results
                                    using the typed New() function
                                    rather than GetMem() which knows
                                    nothing about the "type" of memory
                                    required by the pointer you are
                                    initialising and just blithely
                                    allocates the specified number of
                                    bytes...:<o:p></o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal">Instead of
                                    &gt;&gt; &nbsp; LogData := GetMem( ... );
                                    &nbsp; &nbsp; &nbsp;<o:p></o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"
                                    style="margin-bottom:12.0pt">Use &nbsp; &nbsp;
                                    &nbsp; &nbsp; &nbsp;&gt;&gt; &nbsp; New( LogData );<br>
                                    <br>
                                    And see if you get better results.
                                    &nbsp;:)<br>
                                    <br>
                                    (Also, don't forget to use
                                    "Dispose()" to deallocate the memory
                                    obtained with "New()", rather than
                                    FreeMem())<o:p></o:p></p>
                                </div>
                                <div>
                                  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                                  <div>
                                    <p class="MsoNormal">On 28 August
                                      2011 21:33, David Moorhouse (DUG)
                                      &lt;<a moz-do-not-send="true"
                                        href="mailto:delphi@moorhouse.net.nz"
                                        target="_blank">delphi@moorhouse.net.nz</a>&gt;
                                      wrote:<o:p></o:p></p>
                                    <p class="MsoNormal">I believe it is
                                      a compiler error and will raise a
                                      QA ticket<br>
                                      <br>
                                      Thanks for your &nbsp;help<br>
                                      <span style="color:#888888"><br>
                                        <br>
                                        D</span><o:p></o:p></p>
                                    <div>
                                      <div>
                                        <p class="MsoNormal"><br>
                                          <br>
                                          On 26/08/11 08:17, Peter
                                          Ingham wrote:<br>
                                          &gt; Try filling LogData with
                                          binary zeros after the
                                          Getmem&amp; &nbsp;before the
                                          assign.<br>
                                          &gt;<br>
                                          &gt; FillChar (LogData^,
                                          sizeof(TLogData), 0);<br>
                                          &gt;<br>
                                          &gt; I believe the
                                          uninitialized memory is
                                          messing up the compiler magic<br>
                                          &gt; associated with the
                                          dynamic array.<br>
                                          &gt;<br>
                                          &gt;<br>
                                          &gt; Any reason the local
                                          Tlogdata record is referenced
                                          via a pointer?<br>
                                          &gt;<br>
                                          &gt; I suspect the following
                                          will also work:<br>
                                          &gt; procedure
                                          TUserClass.Log(const LogType:
                                          TLogType; const Args: array of<br>
                                          &gt; const );<br>
                                          &gt; &nbsp; &nbsp;var<br>
                                          &gt; &nbsp; &nbsp; &nbsp; LogData: TLogData;<br>
                                          &gt; &nbsp; &nbsp; begin<br>
                                          &gt; &nbsp; &nbsp; &nbsp; LogData.LogType :=
                                          LogType;<br>
                                          &gt; &nbsp; &nbsp; &nbsp; LogData.LogArgs :=
                                          CreateConstArray(Args);<br>
                                          &gt; &nbsp; &nbsp; &nbsp; // &nbsp;... do some
                                          other stuff with the LogData
                                          item finally calling<br>
                                          &gt; &nbsp; &nbsp; end;<br>
                                          &gt;<br>
                                          &gt; Cheers<br>
                                          &gt;<br>
                                          &gt; On 26/08/2011 1:49 a.m.,
                                          David Moorhouse wrote:<br>
                                          &gt;&gt; Hi Peter<br>
                                          &gt;&gt;<br>
                                          &gt;&gt; Been there done that
                                          :)<br>
                                          &gt;&gt;<br>
                                          &gt;&gt; The function call is
                                          fine. &nbsp;It is the assignment
                                          that causes the AV -<br>
                                          &gt;&gt; because the "bucket"
                                          is too small.<br>
                                          &gt;&gt; Assigning it with 16
                                          bytes fixes the problem,
                                          regardless of how many<br>
                                          &gt;&gt; items the array
                                          holds.<br>
                                          &gt;&gt;<br>
                                          &gt;&gt; I smell compiler
                                          magic in the background.<br>
                                          &gt;&gt;<br>
                                          &gt;&gt; Cheers<br>
                                          &gt;<br>
                                          &gt;&gt; D<br>
                                          &gt;&gt;<br>
                                          &gt;&gt; On 25/08/11 17:29,
                                          Peter Ingham wrote:<br>
                                          &gt;&gt;&gt; Another attempt
                                          to reply...<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; First thing to do
                                          is determine if the crash
                                          occurs in the procedure call,<br>
                                          &gt;&gt;&gt; on the subsequent
                                          assign, or in between.<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; Give this a try:<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;procedure
                                          TUserClass.Log(const LogType:
                                          TLogType; const Args: array of<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;const );<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;var<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp;LogData:
                                          PLogData;<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; TempArgs :
                                          TConstArray;<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;begin<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;// size of
                                          record TLogData does not work<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;GetMem(LogData,
                                          sizeof(TLogData));<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogData.LogType := LogType;<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;// blows up on
                                          one of these lines<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;TempArgs
                                          &nbsp;:= CreateConstArray(Args);<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogData.LogArgs := TempArgs;<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;// &nbsp;... do
                                          some other stuff with the
                                          LogData item finally calling<br>
                                          &gt;&gt;&gt; FreeMem<br>
                                          &gt;&gt;&gt; &nbsp; &nbsp;end;<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; Regarding the
                                          size of a dynamic array, &nbsp;like
                                          a string variable, &nbsp;the<br>
                                          &gt;&gt;&gt; variable (LogArgs
                                          in this case) is the size of a
                                          pointer (i.e. 4 bytes<br>
                                          &gt;&gt;&gt; for Win32). &nbsp;If
                                          the pointer is non-zero, it
                                          points to a structure which<br>
                                          &gt;&gt;&gt; includes the
                                          adjacent array elements
                                          preceded by a length.<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; One thing to
                                          watch out for is that Getmem
                                          does not clear the allocated<br>
                                          &gt;&gt;&gt; memory, so
                                          LogData after the Getmem call
                                          will contain any old rubbish.<br>
                                          &gt;&gt;&gt; The reference to
                                          LogData.LogArgs in the
                                          assignment may be<br>
                                          &gt;&gt;&gt; dereferencing a
                                          non-zero pointer&amp; &nbsp;
                                          attempting to use whatever it<br>
                                          &gt;&gt;&gt; contains.<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; Cheers<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;<br>
                                          &gt;&gt;&gt; On 25/08/2011
                                          11:40 a.m., David Moorhouse
                                          (DUG) wrote:<br>
                                          &gt;&gt;&gt;&gt; I have the
                                          following code snippet<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; &lt;code&gt;<br>
                                          &gt;&gt;&gt;&gt; type<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp;
                                          &nbsp;PConstArray = ^TConstArray;<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp;
                                          &nbsp;TConstArray = array of
                                          TVarRec;<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; function
                                          CreateConstArray(const
                                          Elements: array of const):
                                          TConstArray;<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; type<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;TLogType
                                          = (ltError, ltWarn, ltInfo);<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;PLogData
                                          = ^TLogData;<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;TLogData
                                          = record<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogType: TLogType;<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogArgs: TConstArray;<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;end;<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; ....<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; procedure
                                          TUserClass.Log(const LogType:
                                          TLogType; const Args: array of<br>
                                          &gt;&gt;&gt;&gt; const );<br>
                                          &gt;&gt;&gt;&gt; var<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;LogData:
                                          PLogData;<br>
                                          &gt;&gt;&gt;&gt; begin<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp;//
                                          size of record TLogData does
                                          not work<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;GetMem(LogData,
                                          sizeof(TLogData));<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogData.LogType := LogType;<br>
                                          &gt;&gt;&gt;&gt; // blows up
                                          on next line<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;LogData.LogArgs :=
                                          CreateConstArray(Args);<br>
                                          &gt;&gt;&gt;&gt; // &nbsp;... do
                                          some other stuff with the
                                          LogData item finally calling<br>
                                          &gt;&gt;&gt;&gt; FreeMem<br>
                                          &gt;&gt;&gt;&gt; end;<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; function
                                          CreateConstArray(const
                                          Elements: array of const):
                                          TConstArray;<br>
                                          &gt;&gt;&gt;&gt; var<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;I:
                                          Integer;<br>
                                          &gt;&gt;&gt;&gt; begin<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp;
                                          &nbsp;SetLength(Result,
                                          Length(Elements));<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;for I :=
                                          Low(Elements) to
                                          High(Elements) do<br>
                                          &gt;&gt;&gt;&gt; &nbsp; &nbsp; &nbsp;
                                          &nbsp;Result[I] := &nbsp;// assign a
                                          TVarRec here<br>
                                          &gt;&gt;&gt;&gt; end;<br>
                                          &gt;&gt;&gt;&gt; &lt;/code&gt;<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; The code that
                                          assigns the memory only
                                          assigns 8 bytes - and an
                                          access<br>
                                          &gt;&gt;&gt;&gt; violation
                                          ensues. &nbsp;If I replace the call
                                          to "sizeof" with the number
                                          16,<br>
                                          &gt;&gt;&gt;&gt; the code
                                          works fine.<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; My
                                          understanding of dynamic
                                          arrays was that the compiler
                                          created a 4<br>
                                          &gt;&gt;&gt;&gt; byte<br>
                                          &gt;&gt;&gt;&gt; field before
                                          the first element that
                                          contained the length of the
                                          array.<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; So why does
                                          the sizeof &nbsp;function not
                                          reflect this ? &nbsp;And why do I<br>
                                          &gt;&gt;&gt;&gt; need 16<br>
                                          &gt;&gt;&gt;&gt; bytes not 12
                                          &nbsp;(4 for LogType + 4 for length
                                          of array + 4 for array<br>
                                          &gt;&gt;&gt;&gt; pointer)?<br>
                                          &gt;&gt;&gt;&gt; Also
                                          regardless of the number of
                                          items in the open array
                                          parameter, 16<br>
                                          &gt;&gt;&gt;&gt; bytes works,
                                          so it does not relate the
                                          length of the TConstArray.<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; Your thoughts
                                          ?<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt; David<br>
                                          &gt;&gt;&gt;&gt;<br>
                                          &gt;&gt;&gt;&gt;<o:p></o:p></p>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
                  </div>
                </div>
              </div>
              <p class="MsoNormal"><br>
                _______________________________________________<br>
                NZ Borland Developers Group - Delphi mailing list<br>
                Post: <a moz-do-not-send="true"
                  href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
                Admin: <a moz-do-not-send="true"
                  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 moz-do-not-send="true"
                  href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a>
                with Subject: unsubscribe<o:p></o:p></p>
            </div>
            <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: <a class="moz-txt-link-abbreviated" href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a>
Admin: <a class="moz-txt-link-freetext" href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a>
Unsubscribe: send an email to <a class="moz-txt-link-abbreviated" href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe</pre>
    </blockquote>
    <br>
  </body>
</html>