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.

Singham said:   1 decade ago
Answer should be 28, because decimal = 16, int = 4, single = 4.

God Tuss Great ho said:   1 decade ago
Answer is correct. Beacause each type is behave like a integer and integer size should be 4 so answer is 12.

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?

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.

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

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?

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.

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));

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.

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.


Post your comments here:

Your comments will be displayed after verification.