C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Yes / No Questions (Q.No. 12)
12.
If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?
struct ex
{
char ch;
int i;
long int a;
};
Answer: Option
Explanation:
A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.
Discussion:
18 comments Page 1 of 2.
Venkatesh said:
1 decade ago
answer is 8 Bytes. Because here structure padding is coming to picture.
1. Byte memory will allocate first.
2. Bytes allocate for int but after char int will stars from multiple of 2 memory location till now structure size is (1char+1 int padding+2 int)=4 bytes.
Now 4 bytes will allocate for long here no structure padding is required because here memory will starts from multiples of 4 already, So instantly 4 bytes memory for long will allocate for long.
So total size is:(1char+1 for int padding+2 int+4 for long) = 8 bytes.
1. Byte memory will allocate first.
2. Bytes allocate for int but after char int will stars from multiple of 2 memory location till now structure size is (1char+1 int padding+2 int)=4 bytes.
Now 4 bytes will allocate for long here no structure padding is required because here memory will starts from multiples of 4 already, So instantly 4 bytes memory for long will allocate for long.
So total size is:(1char+1 for int padding+2 int+4 for long) = 8 bytes.
Sundar said:
1 decade ago
@Rajendra Singh
The size of the struct is the amount of memory the compiler chooses to devote to it. Different compilers make different choices, because they are concerned with different constraints -- more generally, with different "figures of merit".
The implementation is free to do what it wants, but usually it occupies additional memory based on the alignment requirements of the machine.
I hope the given answer is correct.
The size of the struct is the amount of memory the compiler chooses to devote to it. Different compilers make different choices, because they are concerned with different constraints -- more generally, with different "figures of merit".
The implementation is free to do what it wants, but usually it occupies additional memory based on the alignment requirements of the machine.
I hope the given answer is correct.
MAHESH said:
5 years ago
Yes, I agree @Venkatesh.
#include<stdio.h>
struct ex
{
char ch;
int i;
long int a;
};
void main()
{
struct ex x;
printf("%d",sizeof(x));
}
answer is:16 for 64 bit,8 for 32 bit;
It allocates memory like char 2 bytes(1 for char + 1 for padding(holes)), and 2 for int and long int takes 4 bytes so, total it takes 8 bytes answer is NO;.
#include<stdio.h>
struct ex
{
char ch;
int i;
long int a;
};
void main()
{
struct ex x;
printf("%d",sizeof(x));
}
answer is:16 for 64 bit,8 for 32 bit;
It allocates memory like char 2 bytes(1 for char + 1 for padding(holes)), and 2 for int and long int takes 4 bytes so, total it takes 8 bytes answer is NO;.
Vikas dubey said:
1 decade ago
I dont know this contradiction, But I know that stucture occupies memory depending on the size of members of the class, where as union occupies largest member size...7 is correct one....internal memory occupation is compiler dependent.
Srinivas bandreddy said:
8 years ago
#include<stdio.h>
struct ex
{
char ch;
int i;
long int a;
};
void main()
{
struct ex x;
printf("%d",sizeof(x));
}
This output is 7 ,So answer is A.
struct ex
{
char ch;
int i;
long int a;
};
void main()
{
struct ex x;
printf("%d",sizeof(x));
}
This output is 7 ,So answer is A.
Rajendra Singh said:
1 decade ago
There was a problem with answer of question number 11. It's output result should be 7. So the answer will option "A" means TRUE. It was tested on TURBO C++ compiler...
Leenaja said:
1 decade ago
Yes Tapas you are right.
If we consider array variable to struct.
Struct ex e[3], then totally it occupies 21 bytes. So structure can occupy other than 7 bytes also.
If we consider array variable to struct.
Struct ex e[3], then totally it occupies 21 bytes. So structure can occupy other than 7 bytes also.
M.Aparna Shri said:
1 decade ago
I also hope the given answer is correct.Because structure allocates memory in same location with required number of bytes.I too agree that the answer is Yes not No.
Tapas said:
1 decade ago
When we declare a structure no memory is allocated but when we create a data type of that structure i.e. now if we write struct ex e; then 7 byte will be reserved.
Amit Saxena said:
9 years ago
I agree to the answer "yes".
The contradiction mentioned above didn't work with the most compiler. So the answer should be yes.
The contradiction mentioned above didn't work with the most compiler. So the answer should be yes.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers