面试题--特别是字节对齐
來源:http://www.cnblogs.com/Braveliu/archive/2013/01/04/2844757.html
【1】設置或者清除某位。
示例代碼如下:
1 #include<iostream> 2 using namespace std; 3 4 #define BIT3 (0x1<<3) 5 6 void Set_bit3(int &a) 7 { 8 a|=BIT3; 9 } 10 11 void Clear_bit3(int &a) 12 { 13 a&=~BIT3; 14 } 15 16 void main() 17 { 18 int m=10; //1010 19 Set_bit3(m); 20 cout<<m<<endl;//1010 == 10 21 Clear_bit3(m); 22 cout<<m<<endl; //0010 == 2 23 24 int n=7; 25 Set_bit3(n); 26 cout<<n<<endl; //1111 == 15 27 Clear_bit3(n); 28 cout<<n<<endl; //0111 == 7 29 } 30 /* 31 10 32 2 33 15 34 7 35 */
【2】指針引用經典筆試題
(1)
1 #include<iostream.h> 2 #include<malloc.h> 3 #include<string.h> 4 5 void GetMemory(char *p,int num) 6 { 7 p=(char *)malloc(sizeof(char)*num); 8 } 9 10 void main() 11 { 12 char *str = NULL; 13 GetMemory(str,100); 14 strcpy(str,"hello"); 15 } 16 17 //GetMemory函數僅僅只改變了p指針的指向,而str的指向不變 18 //類似于: 19 //char *str=NULL; 20 //char *p = str; 21 //p = (char *)malloc(sizeof(char)*num); 22 //那么這樣的三行代碼執行結果顯然沒有達到改變str的作用
(2)
1 #include<iostream.h> 2 #include<string.h> 3 #include<malloc.h> 4 5 void GetMemory2(char **p,int num) 6 { 7 *p=(char *)malloc(sizeof(char)*num); 8 } 9 10 void main() 11 { 12 char *str = NULL; 13 GetMemory2(&str,100); 14 strcpy(str,"hello"); 15 cout<<str<<endl; 16 free(str); 17 } 18 19 //GetMemory函數“隔山打牛” 20 //類似于: 21 //char *str=NULL; 22 //char **p=&str; 23 //*p=(char *)malloc(sizeof(char)*num); 24 //那么這樣的三行代碼執行結果顯然達到改變str的作用
(3)
1 #include<iostream.h> 2 #include<malloc.h> 3 #include<string.h> 4 5 char * GetMemory3(int num) 6 { 7 char *p=(char *)malloc(sizeof(char)*num); 8 return p; 9 } 10 11 void main() 12 { 13 char *str=NULL; 14 str=GetMemory3(100); 15 strcpy(str,"hello"); 16 cout<<str<<endl; 17 free(str); 18 } 19 20 //GetMemory函數功在當代,利在千秋 21 //盡管p變量在棧中,它當生則生,當死則死 22 //但是它在世的一瞬間,卻創造了舉世的成就,開天辟地!! 23 //p=(char *)malloc(sizeof(char)*num);一句足矣 24 //返回p的值,意義無量!
(4)
1 #include<iostream.h> 2 #include<string.h> 3 #include<malloc.h> 4 5 char *GetString() 6 { 7 char p[]="hello world"; 8 return p; //編譯器警告!!!返回局部變量 9 } 10 11 void main() 12 { 13 char *str=NULL; 14 str=GetString(); 15 cout<<str<<endl; //垃圾 16 } 17 18 //GetMemory函數山寨版 19 //p變量在棧中,當生則生,當死則死 20 //但是它在世的一瞬間,卻目光短淺,于后世無功! 21 //返回p的值,一文不值!
(5)
1 #include<iostream.h> 2 #include<string.h> 3 #include<malloc.h> 4 5 char * GetString() 6 { 7 char *p="hello world!"; 8 return p; 9 } 10 11 void main() 12 { 13 char *str=NULL; 14 str=GetString(); 15 cout<<str<<endl; 16 } 17 18 //GetMemory函數正版 19 //p變量在棧中,當生則生,當死則死 20 //但是它在世的一瞬間,卻賦予了一份常量,不隨它的消失而泯滅!
(6)
1 #include<iostream> 2 #include<string> 3 #include<malloc.h> 4 using namespace std; 5 6 void GetString(char *&p) 7 { 8 p=(char *)malloc(sizeof(char)*10); 9 } 10 11 void main() 12 { 13 char *str=NULL; 14 GetString(str); 15 strcpy(str,"hello"); 16 cout<<str<<endl; 17 free(str); 18 str=NULL; 19 }
以上幾種例子是面試時遇到的最頻繁的試題,在此特意備份,以便學習。
【3】這道題是最典型的數組越界示例:
1 #include<iostream.h> 2 #define MAX 255 3 void main() 4 { 5 unsigned char A[MAX]; 6 for(int i = 0; i <= MAX; i++) 7 { 8 A[i] = i; 9 } 10 }
無限循環.......
【4】求最大字段和
示例代碼如下:
1 #include<iostream> 2 using namespace std; 3 4 inline int Max(int a,int b) 5 { 6 return a > b ? a : b; 7 } 8 9 int MaxSubSum(int br[],int n) 10 { 11 int data= 0,max= 0; 12 for(int i=0 ;i<n ;++i) 13 { 14 data = Max(0,br[i]); 15 max = Max(data+max,max); 16 } 17 return max; 18 } 19 20 void main() 21 { 22 int ar[] = {1,-4,-2,-1,7,-3,9}; 23 int result1 = MaxSubSum(ar,7); 24 cout<<result1<<endl; //17 25 }
【5】字節對齊
示例代碼:
1 #include<iostream.h> 2 class A 3 { 4 public: 5 int i; 6 }; 7 class B 8 { 9 public: 10 char ch; 11 }; 12 class C 13 { 14 public: 15 int i; 16 short j; 17 }; 18 class D 19 { 20 public: 21 int i; 22 short j; 23 char ch; 24 }; 25 class E 26 { 27 public: 28 int i; 29 int ii; 30 short j; 31 char ch; 32 char chr; 33 }; 34 class F 35 { 36 public: 37 int i; 38 int ii; 39 int iii; 40 short j; 41 char ch; 42 char chr; 43 }; 44 class G 45 { 46 public: 47 int i; 48 int ii; 49 short j; 50 char ch; 51 char chr; 52 int iii; 53 }; 54 class H 55 { 56 public: 57 int i; 58 int ii; 59 char ch; 60 char chr; 61 int iii; 62 short j; 63 }; 64 class I 65 { 66 public: 67 int i; 68 char ch; 69 int ii; 70 char chr; 71 int iii; 72 short j; 73 }; 74 void main() 75 { 76 cout<<"sizeof(int): "<<sizeof(int)<<endl; 77 cout<<"sizeof(short): "<<sizeof(short)<<endl; 78 cout<<"sizeof(char): "<<sizeof(char)<<endl; 79 cout<<endl; 80 cout<<"sizeof(A): "<<sizeof(A)<<endl; 81 cout<<"sizeof(B): "<<sizeof(B)<<endl; 82 cout<<"sizeof(C): "<<sizeof(C)<<endl; 83 cout<<"sizeof(D): "<<sizeof(D)<<endl; 84 cout<<"sizeof(E): "<<sizeof(E)<<endl; 85 cout<<"sizeof(F): "<<sizeof(F)<<endl; 86 cout<<"sizeof(G): "<<sizeof(G)<<endl; 87 cout<<"sizeof(H): "<<sizeof(H)<<endl; 88 cout<<"sizeof(I): "<<sizeof(I)<<endl; 89 } 90 91 //Out put: 92 /* 93 sizeof(int): 4 94 sizeof(short): 2 95 sizeof(char): 1 96 97 sizeof(A): 4 98 sizeof(B): 1 99 sizeof(C): 8 100 sizeof(D): 8 101 sizeof(E): 12 102 sizeof(F): 16 103 sizeof(G): 16 104 sizeof(H): 20 105 sizeof(I): 24 106 */
【6】大小端判斷
題目:請寫一個C函數,若處理器是Big_endian的,則返回0;若是Little_endian的,則返回1
分析:
 為什么會有大小端模式之分呢?這是因為在計算機系統中,我們是以字節為單位的,每個地址單元都對應著一個字節,一個字節為 8bit。
但是在C語言中除了8bit的char之外,還有16bit的short型,32bit的long型(要看具體的編譯器),
另外,對于位數大于 8位的處理器,例如16位或者32位的處理器,由于寄存器寬度大于一個字節,那么必然存在著一個如何將多個字節安排的問題。
因此就導致了大端存儲模式和小端存儲模式。
例如一個16bit的short型x,在內存中的地址為0x0010,x的值為0x1122,那么0x11為高字節,0x22為低字節。
對于大端模式,就將0x11放在低地址中,即0x0010中,0x22放在高地址中,即0x0011中。
小端模式,剛好相反。我們常用的X86結構是小端模 式,而KEIL C51則為大端模式。
嵌入式開發對大小端都比較敏感。所謂的大端模式是指:
數據的低位(就是權值較小的后面那幾位)保存在內存的高地址中,而數據的高位,保存在內存的低地址中。
這樣的存儲模式有點兒類似于把數據當作字符串順序處理:地址由小向大增加,而數據從高位往低位放;
所謂的小端模式是指:數據的低位保存在內存的低地址中,而數據的高位保存在內存的高地址中,
這種存儲模式將地址的高低和數據位權有效地結合起來,高地址部分權值高,低地址部分權值低,和我們的實際邏輯方法一致。
示例代碼如下:
1 #include<iostream> 2 using namespace std; 3 4 int CheckCpu() 5 { 6 union w 7 { 8 int a; 9 char b; 10 }c; 11 c.a=1; 12 return (c.b==1); 13 } 14 15 void main() 16 { 17 cout<<CheckCpu()<<endl; //1 //說明是小端模式 18 }
總結:
其實大小端的問題很簡單的,就是因為數據在同樣的內存可以有兩種存儲模式。
簡單記住:低低小,低高大。
也就是說:低位數據存入低地址小端;低位數據存入高地址大端。
【7】求整型數組中的最小以及次小項
示例代碼如下:
1 #include<iostream> 2 using namespace std; 3 4 void select(int ar[],int n) 5 { 6 int m1, m2; 7 m1 = m2 = 0xffff; 8 int x1, x2; 9 x1 = x2 = 0; 10 for(int j = 0; j < n ;j++) 11 { 12 if (ar[j] < m1) 13 { 14 m2 = m1; //暫存次小 15 x2 = x1; //暫存次小索引 16 m1 = ar[j]; //暫存最小 17 x1 = j; //暫存最小索引 18 } 19 else if(ar[j] < m2 ) 20 { 21 m2 = ar[j]; //保存次小 22 x2 = j; //保存次小索引 23 } 24 } 25 cout<<x1<<" "<<m1<<endl; //輸出最小 26 cout<<x2<<" "<<m2<<endl; //輸出次小 27 } 28 void main() 29 { 30 int ar[5] = {1, 7, 5, 4, 2}; 31 select(ar,5); 32 } 33 34 /*運行結果: 35 0 1 36 4 2 37 */
轉載于:https://www.cnblogs.com/heyonggang/archive/2013/04/15/3021926.html
總結
以上是生活随笔為你收集整理的面试题--特别是字节对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 奥比甜甜小馋猫表情,值多少?
 - 下一篇: 《凉夜有怀》第七句是什么