C# Programming - Structures - Discussion

Discussion Forum : Structures - General Questions (Q.No. 7)
7.
How many bytes will the structure variable samp occupy in memory if it is defined as shown below?
class Trial
{ 
    int i; 
    Decimal d;
}
struct Sample
{
    private int x; 
    private Single y; 
    private Trial z;
}
Sample samp = new Sample();
20 bytes
12 bytes
8 bytes
16 bytes
24 bytes
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Devendra said:   6 years ago
As I can see here, only @Joao Lourenco replied with logical answer.

I'm going to explain the answer as simple as I can, hope it will clear confusion.

1: int is of 4 bytes. Its fixed no discussion required.
2: class reference variable is totally depends on your system structure. If you are using 32 bit machine then reference size will be 32/8 = 4 bytes, but if you are using 64 bit machine then reference size will be 64/8 = 8 bytes.
3: Single is a kind of variable type like int or string. Here single is equivalent to float and default size of float is 4 bytes.

So the conclusion is, if you are using 32 bit machine the size will be 4 + 4 + 4 = 12 bytes, but if you are using 64 bit machine then size will be 4 + 4 + 8 = 16 byes.

If you are confuse about how single is equivalent to float then have a look in below program,

using System;

class Program
{
static void Main()
{
{
Single a = 1;
Console.WriteLine("Single variable type: " + a.GetType());
}
{
float a = 1;
Console.WriteLine("float variable type: " + a.GetType());
}
}
}

Output:

Single variable type: System.Single
float variable type: System.Single

Ibtisam said:   6 years ago
@Ramprasad.

Why is reference to trial = 4?

Vaibhav said:   9 years ago
Because in the structure we don't have a decimal, we have a reference. And a reference will be of 4 bytes no matter the size of the class. Hence 4+4+4 = 12.

Ramprasad said:   10 years ago
int = 4, single = 4, reference to trail = 4.

Actual Trial lives in Heap. But the reference is with struct in Stack.

So answer is 12.

Andrew said:   1 decade ago
Console.WriteLine("int = {0}, decimal = {1}, single = {2}, summa = {3}", sizeof(int), sizeof(Decimal), sizeof(Single), (sizeof(int) + sizeof(Decimal)) + sizeof(int) + sizeof(Single));

Ashish Kumar Mishra said:   1 decade ago
z= 4, int = 4, single = 4.

Here z is reference pointing to Trial object. in.Net every reference occupy 4 bytes. So answer is 12 Bytes.

Sumit Sood said:   1 decade ago
Don't know why it is so, but on executing the problem it return size = 12. And I am not satisfied with the answer on fetching size of each member it is giving 4, 4, 4, 16. But in total it is not counting size of decimal member. Confused?

Priyali said:   1 decade ago
Can someone tell me what is the correct answer please?

NoNam said:   1 decade ago
With the Marshal.SizeOf it says that 16 bytes are allocates. However is not a correct way to know, bu it have sense. Each variable (default values) weights 4 bytes. We have 3 in the struct Sample, but Trial object counts for 2 because contains two members.

Joao Lourenco said:   1 decade ago
Surely this depends on the architecture? If you are in a 64-bit environment, the size should be 16 (64bits = 8bytes) because of the reference to the class?


Post your comments here:

Your comments will be displayed after verification.