Results 1 to 4 of 4

Thread: getting the size of a structure?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    getting the size of a structure?

    what if you have a structure like this:

    struct foo
    {
    public int A;
    public char[] B;
    }

    the size of the structure obviously changes depending on the size of the char array inside it. Is there a "correct" way to get the size of the struct so that it would include the size of the character array?
    sizeof() obviously will not work... i could check the length of the array at runtime to get the size of the struct, but i was just wondering if there is a function for doing this already
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: getting the size of a structure?

    aah Marshal.SizeOf ()
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: getting the size of a structure?

    hmm actually for some reason Marshall.SizeOf() seems to only count 4 bytes for the character array regardless of how many items it has in it
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: getting the size of a structure?

    The size of that structure does NOT change depending on the size of the array. Arrays are reference type objects, so the structure only contains a reference to the array. That reference is 32-bits no matter how many elements the array has. If you want the size of the array then you need to multiply its Length by the size of its element type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width