Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
What is the size of  the following structure?
typedef  struct
{
   int ctrl1;
   char flag;
   char flag2;
   int ctrl2;
   int ctrl3;
} MYREG;


a)14
b)20
c)24
d)16
Posted
Updated 21-Jan-13 0:29am
v2

1 solution

Depends on a number of factors, primarily which compiler you use.
If you use an old 16 bit compiler, then an int is 2 bytes.
If you use a more modern compiler, then an int can be 4 or 8 bytes (I've yet to see a C compiler that used 128 bit integers).

In addition, it will depend on your compiler options - some will "pack" the structure by default, others won't. If they do pack it, it may depend on the target processor and the compiler efficiency - some combinations will compact the two characters to successive bytes, and the following int into the next two bytes, others will require each int to start on a 4 byte boundary.

So, Compile it, and try it. It isn't difficult for you to come up with a definitive answer for your configuration.
 
Share this answer
 
Comments
Rahul Rajat Singh 21-Jan-13 7:02am    
I would have said "Result would depend on compiler and compile options" but this answer is simply superb. +5.
OriginalGriff 21-Jan-13 7:09am    
:blush:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900