sizeof(enum_type)

面试时候碰到。还有就是如何防止头文件重复定义,用
#ifndef
#def
#enddef
野指针,free后再置为NULL。

sizeof(enum_type)
C++标准p692:
In C++,the type of an enumerator is its enumeration. In C,tye type of an enumerator is int.
   enum e{A};
   sizeof(A)==sizeof(int)   //in C
   sizeof(A)==sizeof(e)    //In C++
   /*and sizeof(int) is not nessary equal to sizeof(e) */

  那么怎么计算enumerator的sizeof?
the c++ programming language 3rd edition 4.8
The range of an enumeration holds all the enumeration’s enumerator values rounded up to the nearest larger binary power minus 1. The range goes down to 0 if the smallest enumerator is nonnegative and to the nearest lesser negative binary power if the smallest enumerator is negative. This defines the smallest bitfield capable of holding the enumerator values. For example:
   enum e1 { dark, light}; / / range 0:1
   enum e2 { a = 3, b = 9}; / / range 0:15
   enum e3 {min= 10, max = 1000000 }; / / range 1048576:1048575


The sizeof an enumeration is the sizeof some integral type that can hold its range and not larger than sizeof(int), unless an enumerator cannot be represented as an int or as an unsigned int. For example, sizeof(e1) could be 1 or maybe 4 but not 8 on a machine where sizeof(int)==4.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值