Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
One day, I was asked to answer a question about struct, it almost like this:
C#
typedef struct TAG_DATA
{
    int type:2;
    int count:12;
    int reserved;
}DATA;
int main(int argc, char* argv[])
{
    DATA apple;
    apple.type  = 0x7;
    apple.count = 0x16;

    int *pValue;
    pValue = (int*)(&apple);
    printf("%x\n",*pValue);

    return 0;
}


The result printed will be( ? )
A:0x13 B:0x43
C:0x1700 D:0x4300(Not very sure about this item,but sth approximately)

I can't give an answer at that moment, and when I come back home, I still found it's hard to figure out any answer of A,B,C,or D, In my program, ouput is 0xccccc05b, why ?

Who can tell me something about this kind of value assign? Thanks in advance!
Posted
Comments
Alain Rist 15-Mar-11 3:38am    
There is no A, B, C or D in your posted code.
OriginalGriff 15-Mar-11 3:41am    
The multiple-choice answers are labelled A, B, C, and D.

C++ can do some alignment optimization, hence the answer can be compiler dependent, and even compiler-options dependent.

Normally bit fields are aligned from right to left until a multiple of the processor word (32 bit, but the alignment can be changed with pragmas and switches) is reached (so field is "across words").

A possible layout can be
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
|___________________________________|_______________________|___|
                                     ^count:12               ^type:2

|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
|_______________________________________________________________|
^ int reserved (:32)


If you look at all this as a single int, (*(int*)&value),
type = 7 (111 in binary) will store 11 in type:2 (making it = 3) and
count = 0x16 = 0001_0110 will store it as it is, so you will end-up with

101_1011 = 0x5B. But consider also that there could be a number of uninitialized bits in front, so memset the data variable to all-zero before doing anything else.
 
Share this answer
 
Comments
scu_sundy 15-Mar-11 4:52am    
Thanks, Emilio.
I am clear now.
CPallini 15-Mar-11 4:53am    
Very good answer. If you suppose the compiler initializing with 0xCCCCCCCC uninitialized 32 bit words (as VS in debugger build seems to do) then you obtain exactly the OP result.
OriginalGriff 15-Mar-11 4:55am    
Very good - I got the reserved wrong - my 5!
Nuri Ismail 15-Mar-11 11:52am    
This is an excellent answer! 5+
I can't see any reason why the answer should be any of the possible values:
"type" is two bits, so assigning a value of 7 should leave type == 3
"count" is twelve bits, so assigning a value of 0x16 should leave count == 22
"reserved" is unassigned, so the value is random.
Bit breakdown:
Bit number
 3         2         1         0 
10987654321098765432109876543210
..............................xx  type
..................xxxxxxxxxxxx..  count
xxxxxxxxxxxxxxxxxx..............  reserved
Puting the values in:
Bit number
 3         2         1         0
10987654321098765432109876543210
..............................11  type     == 0x03
..................000000010110..  count    == 0x16
xxxxxxxxxxxxxxxxxx..............  reserved == unassigned
xxxxxxxxxxxxxxxxxx00000001011011  Total of "apple"
So Apple will end 0x05B, with some random crap at the top.
Even if for some weird reason the compiler packed the bits the other way
MSIL
Bit number
 3         2         1         0
10987654321098765432109876543210
11..............................  type     == 0x03
..000000010110..................  count    == 0x16
..............xxxxxxxxxxxxxxxxxx  reserved == unassigned
11000000010110xxxxxxxxxxxxxxxxxx  Total of "apple"
Then apple would start with 0xC05 then some random crap.
I think they got it wrong, and maybe the test was: Will he have the confidence to say "You are wrong"?
 
Share this answer
 
Comments
CPallini 15-Mar-11 4:47am    
I guess your memory layout is wrong, since reserved is a full integer (see the Emilio's answer for the correct one).
OriginalGriff 15-Mar-11 4:53am    
Doh! Brain death has occurred - best top up the caffeine levels! The answer still stands: the question is wrong.
CPallini 15-Mar-11 4:55am    
:-)
scu_sundy 15-Mar-11 5:04am    
I think reserved will take up another 32 bit, isn't it?
OriginalGriff 15-Mar-11 5:06am    
Yep! Hence why brain death has occurred! We have a serious coffee shortage in my blood stream - that'll fix it! :laugh:

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