6 Replies Latest reply on Nov 16, 2007 1:32 AM by <newbie />

    Struct Size

    GuyPhempoompanitch Level 1
      I am trying to determine the size of a structure, as in byte size, not structCount() before I include it in an error handling email. Does anyone have a solution that does this?

        • 1. Re: Struct Size
          Grizzly9279 Level 1
          As far as I know, there are no "sizeOf" methods available in Java (or CF as a result), like you would find in languages like C or C++. This is largely due to how much differently Java handles memory allocation than other languages (e.g...heaps, garbage collection, etc)

          Unfortunately, the best you could do would be to write a custom routine to recursively crawl through the structure and and count the number of characters present in all of the strings it contains. (assuming this is a structure of purely "simple" data types). You could then provide a rough estimation on bytes allocated to the struct based on the total sum of characters it contains.
          • 2. Re: Struct Size
            Level 7
            You could WDDX it and then strip the tags out. What would be left
            (basically just the xmltext "nodes") would be the actual data. Do a len()
            on that, to give you a rough idea of the size.

            Although the significant size is not the raw size of the data, but the size
            of the string you're emailing. You must be "handling" the struct somehow
            to serialise it to include in the email text... how are you doing that?

            • 3. Re: Struct Size
              Daverms Level 3

              Intrestingly, When I tried to obtain the size of a structure through an "coldfusion.runtime.struct" object (by calling the "size()" method) it returned the number of elements rather than returning bytes!.

              • 4. Re: Struct Size
                cf_dev2 Level 1
                > Intrestingly, When I tried to obtain the size of a structure ..

                That's what size() is supposed to do. A coldfusion.runtime.struct is a Hashtable. Its size() method is defined here

                • 5. Re: Struct Size
                  Daverms Level 3
                  Hi cf_dev2,

                  Thanks for the explanation and the link, Now I got clarified :-)
                  • 6. Re: Struct Size
                    <newbie /> Level 1

                    This is an interesting problem.

                    Would something like this get you close enough...

                    <!--- create the structure object --->
                    <cfset myStruct = StructNew() />

                    <!--- add one item with a data-type of simple value --->
                    <cfset StructInsert(myStruct, "my_key", "my_value") />

                    <!--- add another with a data-type of array --->
                    <cfset myArray = ArrayNew(1) />
                    <cfset ArrayAppend(myArray, "another_value") />
                    <cfset StructInsert(myStruct, "another_key", myArray) />

                    <!--- number of bytes as a string --->
                    <cfset len = ArrayLen(myStruct.toString().getBytes()) />

                    Good luck!