2010年8月2日 星期一

C++ sizeof使用規則及陷阱分析


http://translate.googleusercontent.com/translate_c?hl=zh-TW&sl=zh-CN&u=http://mxdxm.javaeye.com/blog/510364&prev=/search%3Fq%3Dsizeof%2B%25E8%2580%2583%25E9%25A1%258C%26hl%3Dzh-TW%26rlz%3D1C1_____enTW359TW359&rurl=translate.google.com.tw&twu=1&usg=ALkJrhh8UplXLX055QvpsTOuGrZP8pC3tQ

華寶面試考題 from ptt

Assume architecture 32-bit
1.
sizeof(char)=?//1
sizeof(char*)=?//4
2.
unsigned short *a = (unsigned short*) 0x0008;
unsgined short *b = a + 1;//0x000a
b=?  
                                                                                
3.
int a=1;
int b = (a++) + (++a);//4
b=?
                                                                                
4.
union tt
{
        unsigned short a;
        unsigned short b[2];
        unsigned short c;
}A;
A.a=10;
A.c=20;
A.a=?//20
sizeof(tt)=?//4
                                                                                
5.
int a=3;
do{
        printf("%d\n", a-=2);
}while(!(--a));
console output?//1 -2
                                                                                
                                                                                
6.
int a=0x0c0c;
int b = (a>>4) | (a>>4);
b=?
                                                                                
7.
struct node
{
        int val;
        struct node *next;
}a,b,*p,*q;
p = &a;
q = &b;
請問下列哪個不能linked list
(a)a.next = q; (b) p->next = &b; (c) p.next = q; (d) (*p).next = q;
                                                                                
8.
char a = 45;
以下何者為零?
(a)a|a (b)a&a (c)a^a (d) a!=30  (c)
                                                                                
9.
void f(int a)
{
int b;
switch(a){
        case 1:
                b=1;
                break;
        case 2:
                b=2;
                break;
        case 3:
                b=3;
                //沒有break
        default:
                b=10;
}
return b;
}
printf("%d", f(3));
what is console output?

offsetof

http://blog.linux.org.tw/~jserv/archives/001399.html