Cisco Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Could you please explain the #pragma part . I couldn't understand how the output becomes 3 bytes .
@shondik
Are u sure u r correct?
if yes then plz point out my mistake.in case of fundamental data types the alignment remains the same as size while in case of user defined,alignment is on the basis of the largest element of the struct which here is int(4 byte) so 4+4=8 should be correct
I have implementd the code ,o/p is 8(for 64 bit sys).
@codez : you are correct . Output is 8 (64 bit and 32 bit)
It depends on the machine architecture .
yeah sure. There is a concept called as memory banking which is undertaken by the microprocessor for faster memory access. In INTEL micro-processor, an address always starts from the even one. If it starts from the odd address, then the micro-processor will take two clock cycles to access a memory.
I am not talking about the word alignment in case of fundamental types. Word alignment plays a very vital role in case of use defined types for faster memory access.
Now, coming to your issue. Your output is correct but you are missing some points. Word alignment is never done based upon the size of biggest type. Consider the below example:
struct s
{
int i;
double d;
};
int main()
{
struct s s1;
printf("%d ",sizeof(s1));
return 0;
}
The output is 12 & not 16. See here: ideone.com/uVdlU
One more point must be noted. Alignment is also done based on the structure variables.
#pragma directive can be used to change the word alignment of the user defined type.
Used as: #pragma pack(1).
Hope its clear.
Yeah thnx for the details and correcting me.It appears more clear....It's the word size of processor that matters not the largest size element.
@shondik : plz explain the #pragma concept using an example . the value inside the pack must be the power of 2 .
See this example:
#pragma pack(1)
struct s
{
char c;
int i;
};
int main()
{
printf("%d ",sizeof(struct s));
return 0;
}
The output is 5 because i have changed the alignment to 1 byte.
So, 1byte for char + 4 bytes for int. ideone.com/2Gg6n
Interesting stuff with the pragmas. I've never seen such a thing. It's important to keep in mind, of course, that pragmas are completely nonstandard and options for using them may differ from compiler to compiler. I'm guessing that in the event you say pack(1), the compiler automatically uses 2 cycles to access misaligned memory locations and manipulates the results with some bit shifting to return the correct value.
Answer is 8
But is this process called address alignment or bit alignment/bit padding
I am not sure asking you guys
It depends on the compiler, which tries to optimize layout for the target architecture.
So the answer will vary. Saying there is one answer is wrong, IMO. Compilers have a certain degree of freedom here, and the behavior can also be changed with command line args.
The ISO C standard does not specify required struct sizes, but it does have rules about alignment. (i.e. alignment of a struct has to be a multiple of smallest common multiple of the field alignments).
This is why, if you require a certain packing (as protocol stack and hardware driver structs often do), you may need to add compiler extensions to specify the alignment you need.
For examples, see the packed, align, and bitwise attributes for GCC. They are used throughout the linux kernel.
It depends on the size of the data bus. Alignment always adjusts the starting address to being with a power of 2. When data is transferred from memory to CPU registers, there is a minimum number of bits that can be transferred in a clock cycle. On a 32 bit machine, it is 32 bits. Thus if you have addresses that are not aligned, there would be wastage of bus capacity.
The output depends upon the architecture i.e. word length of the micro-processor.
- Aashish July 01, 2012If the word length is 16 bits, then the output is 4 bytes.
If the word length is 32 bits, then the output is 8 bytes.
One more factor that may affect the output is the word alignment which can be changed by #pragma. By default, in turbo-c, the word alignment is 1 byte.There is no effect of #pragma directive. So, there you will find the output as 3 bytes.