题目:
将题目代码粘贴在下面便于分析:
#define MAX_SIZE A+B struct _Record_Struct { unsigned char Env_Alarm_ID : 4; unsigned char Para1 : 2; unsigned char state; unsigned char avail : 1; }*Env_Alarm_Record; struct _Record_Struct *pointer = (struct _Record_Struct*)malloc(sizeof(struct _Record_Struct) * MAX_SIZE);
错误选型:B
正确选择:D
题目分析:
也即最后malloc( ),括号中的表达式为(3*2+3),等于9个字节.
注意!宏在代码中替换表达式时就是原样替换,不会因为宏定义的整体就替换成带括号的表达式,就像本题,宏替换后的表达式为:
#define MAX_SIZE A+B malloc(sizeof(struct _Record_Struct) * MAX_SIZE); //宏替换后: malloc(sizeof(struct _Record_Struct) * 2 + 3 ); //而不是: malloc(sizeof(struct _Record_Struct) * (2 + 3) );