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 2 of 2.

Ibtisam said:   6 years ago
@Ramprasad.

Why is reference to trial = 4?

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


Post your comments here:

Your comments will be displayed after verification.