Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I dont know if previously someone asked the same. When i tried to search in the net, i couldn't find it. Please help me in solving this?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
            [StructLayout(LayoutKind.Sequential)]
        public class Details
        {
            public uint ID;
            public uint state;
            public uint country;
            public uint place;
        }
        [StructLayout(LayoutKind.Sequential)]
        public class UserDetails
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
            public Details[] userDetails;
        }
        static void Main(string[] args)
        {

            UserDetails u = new UserDetails();
            int sizeofDetails = Marshal.SizeOf(u);
        }
    }
}


When i executed the code, i am expecting the sizeofDetails should be 128. But i am getting 64.

Is there any problem in declaration of the array. Can someone please help?
Posted
Comments
[no name] 1-Sep-14 7:42am    
What would you expect the size of a completely empty array to be? Why would you expect 64 or 128?
_Asif_ 1-Sep-14 8:02am    
Are you compiling your code on x64 platform?

1 solution

Why would you expect it to be 128 bytes? Details is a class, not a struct so an array of Details elements will be an array of references to the elements, not the elements themselves.
Since you are using a 64 bit system, each reference is 8 bytes, and so each array is 8 * 8 bytes == 64 bytes.

You might try making Details a struct instead: then it's a value type and the array will directly contain the elements instead of references to the elements.
 
Share this answer
 
Comments
Rob Philpott 1-Sep-14 13:06pm    
Splendid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900