Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

My question is regarding the size of the class when it contains any variables.
If my class is containing only a variable of Int type ,my class size is of 4.
you can check here:

C#
using System.Runtime.InteropServices;
   class Program
    {
        
        static void Main(string[] args)
        {
                test p = new test();
            Console.WriteLine("Number of bytes needed by a Point object: {0}",
                Marshal.SizeOf(p));
            Console.ReadLine();

         }
    }
    [StructLayout(LayoutKind.Sequential)]
    class  test 
    {
     int i;
     double d;    
    }


It shows 4 when I've only int in my class, and shows 8 if it contains only double variable.
But it shows 16 if one double and one Int variable is there.
Why it showing 16?

I need a clarification .please help me !!

Thanks in Advance
Kabita Nayak
Posted

Because "real" memory isn't organised in bytes: it's organised in units of the processor / OS size: 32 bits or 64 bits.
And a datatype must start on it's "natural" boundary: for a byte, the address can end with anything. For a short (16 bits) it must end with a binary zero. A double (which needs 64 bits, or 8 bytes, wide) needs to start on a memory location that ends with binary 000 in order to be accessed within it's natural boundary.
So when you put an int in front of it, the int is "padded" to allow teh double to hit teh natural boundary.
Try it: put another int in between:
C#
[StructLayout(LayoutKind.Sequential)]
class test
    {
    int i;
    double d;
    }
[StructLayout(LayoutKind.Sequential)]
class test2
    {
    int i;
    int j;
    double d;
    }
You'll find they are both the same size, while this:
C#
[StructLayout(LayoutKind.Sequential)]
class test3
    {
    int i;
    double d;
    int j;
    }
takes another 8 bytes.
 
Share this answer
 
Comments
Member 11023812 30-Aug-14 11:28am    
Hi OriginalGriff,
Thanks for the reply...
yes...You are right..I've tested these found these difference .But unable to understand the concept behind that...
Now I've clear idea on this concept...
Thank you
OriginalGriff 30-Aug-14 11:32am    
You're welcome!
This is because of padding.

The size of a class instance is determined by:

- The amount of data actually stored in the instance
- The padding needed between the values
- Some extra internal data used by the memory management

Please take a look at this anwer: Size of a class in c#[^]
 
Share this answer
 
v2
Comments
Member 11023812 1-Sep-14 1:02am    
Thanks a lot for the reply....
In addition to Solutions 1-2: during runtime, you can always check up how much a structure allocates using System.Runtime.InteropServices.Marshal.SizeOf(System.Object):
http://msdn.microsoft.com/en-us/library/y3ybkfb3%28v=vs.100%29.ASPX[^].

It will give you, essentially, the size you need: umnanaged size in byte. This is the size you would need to allocate in unmanaged memory to fit your object in it. This is a size of a whole continuous block of memory needed to fit it.

So, you need to be careful: this size won't take into account the size of reference-type objects, if you put any of them in the structure; however, it will give your the size needed to have references to those objects in the structure.

—SA
 
Share this answer
 
v2
Comments
Member 11023812 1-Sep-14 1:03am    
Thanks fro the reply!! its really helpful...
Thanks All for the valuable answers...
I'm rally thankful to you all..Now I've a clear idea on this concept..


Thanks All
 
Share this answer
 

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