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();
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
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
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.
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?
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));
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.
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?
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.
Here z is reference pointing to Trial object. in.Net every reference occupy 4 bytes. So answer is 12 Bytes.
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.
Actual Trial lives in Heap. But the reference is with struct in Stack.
So answer is 12.
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.
Singham said:
1 decade ago
Answer should be 28, because decimal = 16, int = 4, single = 4.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers