某公司笔试题:
1.
int *px = new int;
long *py = new long[32];
分别写出指针px和py的内存释放语句:
delete px;
delete py[];
2.
在4字节对齐的情况下:
struct A
{
long lnum; //4byte
char *pcname; //4byte
short int data; // 2byte
char ucstr; // 1byte
short data[5]; //12byte
}*p;
p = 0x1000000;
求:p + 0x200; (long *)p + 0x200; (char *)p + 0x200。
指针和一个整数运算,表示指针按照相应类型格式偏移,首先p+0x200就是按照结构体类型偏移0x200个字节,后面的强制类型转换,则是按照强制类型格式偏移0x200字节。
3.
定义一个无符号短整型:unsigned short a = 0x1234,如何实现变量a的高低8位调换,即b = 0x3412。
很明显要通过位运算来实现:
unsigned short b = a<<8 | a>>8;