天天看點

C程式的存儲布局——代碼段

Code:

  1. #include<stdio.h>  
  2. int f(int a,int b)    
  3. {  
  4.   return a+b;     
  5. }  
  6. int main()  
  7. {  
  8.   int(*p)(int ,int );    
  9.   void *q;          
  10.   p=f;             
  11.   q=(void *)p;      
  12.   printf("the code is : 0x%x", *((int *)q));  
  13.   *((int *)q)=0x12345678;       
  14.   return 0;  
  15. }  

列印的是函數f中的語句“return a+b"

Code:

  1. $ ./motify_code  
  2. the code is : 0x8be58955  
  3. Segmentation fault  

修改正文段時報錯可知:linux環境下的正文段是受作業系統寫保護的。

補充:函數的代碼存儲在代碼段中。

繼續閱讀