开发者

Getting Unexpected result with 'sizeof(union.variable)'->Objective C

开发者 https://www.devze.com 2023-03-06 18:19 出处:网络
I am getting unexpected result for the below code. 开发者_高级运维union { int aBuf[RMH_MAX_UNENCODED_LENGTH+sizeof(MSG_INFO)]; //4070+68=4138

I am getting unexpected result for the below code.

开发者_高级运维union
{
    int aBuf[RMH_MAX_UNENCODED_LENGTH+sizeof(MSG_INFO)]; //4070+68=4138
}sUnion;
NSLog(@"%d",sizeof(sUnion.aBuf));//printing as 16552 and not 4138


That is the correct output for this code. Your union contains an array of 4138 int types. If you ran NSLog(@"%d",sizeof(int));, the output would be 4. 4*4138=16552, so an array of 4138 ints is 16552 bytes long.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号