2014-02-18 50 views
-9

時出現分段錯誤(核心轉儲)這裏是我的代碼。
任何人都可以幫助我嗎?爲什麼在編譯我的代碼C(linux)

#include <SDL/SDL.h> 
#include <stdlib.h> 
#include <stdio.h> 


int blit_image(char chemin[15],SDL_Surface *fenetre,int posx,int posy) { 
    SDL_Surface *temp; 
    SDL_Surface *image; 
    SDL_Rect position; 

    temp = SDL_LoadBMP(chemin); 

    image= SDL_DisplayFormat(temp); 
    position.x = posx; 
    position.y = posy; 
    position.w = image->w; 
    position.h = image->h; 

    SDL_BlitSurface(image,NULL,fenetre,&position); SDL_Flip(fenetre); 
    SDL_FreeSurface(temp); 
    SDL_FreeSurface(image); 
} 

int main(int argc, char *argv[]) { 
    SDL_Surface *fenetre; 
    SDL_Event event; int done=1; SDL_Init(SDL_INIT_VIDEO); 

    fenetre = SDL_SetVideoMode(910,600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); 
    blit_image("resources/images/fondblanc.bmp",fenetre,0,0); 
    blit_image("resources/images/chat.bmp",fenetre,0,0);//cat 
    blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25);//dog 
    blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); //horse 
    blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); //tiger 
    blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370);//hen 
    blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485); 
    while (done) { 

     SDL_WaitEvent(&event); 
     switch(event.type) { 
     case SDL_QUIT: 
      { 
       done=0; 
       break; 
      } 
     case SDL_MOUSEBUTTONDOWN: 
      { 
       if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<115)&&              (event.button.y>25)) 
       { 
        blit_image("resources/images/chat.bmp",fenetre,0,0); 
       } 
       if ((event.button.x>820)&&(event.button.x<890)&&(event.button.y<230)&&   (event.button.y>140)) 
       { 
        blit_image("resources/images/chien.bmp",fenetre,0,0); 
       } 
       if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&&(event.motion.y>255)) 
       { 
        blit_image("resources/images/cheval.bmp",fenetre,0,0); 
       } 
       if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&&   (event.motion.y>370)) 
       { 
        blit_image("resources/images/tigre.bmp",fenetre,0,0); 
       } 
       if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485)) 
       { 
        blit_image("resources/images/poule.bmp",fenetre,0,0); 
       } 
      } 

     } 

     if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<115)&&   (event.motion.y>25)) 
     { 
      blit_image("resources/bouttons/chatbtClic.bmp",fenetre,820,25); 
      blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); 
      blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); 
      blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370); 
      blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485); 
     } 
     if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<230)&&(event.motion.y>140)) 
     { 
      blit_image("resources/bouttons/chienbtClic.bmp",fenetre,820,140); 
      blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25); 
      blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); 
      blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370); 
      blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485); 

     } 
     if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<345)&&   (event.motion.y>255)) 
     { 
      blit_image("resources/bouttons/chevalbtClic.bmp",fenetre,820,255); 
      blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25); 
      blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); 
      blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370); 
      blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485); 

     } 

     if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<460)&&(event.motion.y>370)) { 
      blit_image("resources/bouttons/tigrebtClic.bmp",fenetre,820,370); 

      blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25); 
      blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); 
      blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); 

      blit_image("resources/bouttons/poulebt.bmp",fenetre,820,485); 
     } 
     if ((event.motion.x>820)&&(event.motion.x<890)&&(event.motion.y<575)&&(event.motion.y>485)) 
     { 
      blit_image("resources/bouttons/poulebtClic.bmp",fenetre,820,485); 

      blit_image("resources/bouttons/chatbt.bmp",fenetre,820,25); 
      blit_image("resources/bouttons/chienbt.bmp",fenetre,820,140); 
      blit_image("resources/bouttons/chevalbt.bmp",fenetre,820,255); 
      blit_image("resources/bouttons/tigrebt.bmp",fenetre,820,370); 

     } 
    } 

    SDL_Quit(); 

    return 0; 
} 

我用gcc game.c -o prog -lSDL -lSDL_mixer -lSDL_ttf

代碼編譯succeffully但上鍵入./prog執行它顯示我"Segmentation fault (core dumped)"

代碼編譯它尚未存在於目錄中使用的文件

+3

在像gdb這樣的調試器中運行該程序,並查看它崩潰的位置 – iain

+2

*「分段錯誤(核心轉儲)」*不是編譯器錯誤(除非發現編譯器錯誤,這是不太可能的)。相反,它更可能是你的*運行時*錯誤。描述你的確切步驟。你編譯過代碼嗎?你鏈接的代碼?你是否運行過代碼?你究竟在哪裏得到錯誤,你是如何嘗試調試它的? – abelenky

+0

我使用gcc game.c編譯了它-o prog -lSDL -lSDL_mixer -lSDL_ttf – sanaEsprit

回答

3

你聲明的功能:

int blit_image(char chemin[15],SDL_Surface *fenetre,int posx,int posy) { 

,使得參數#1有15個字符。

但是,當你調用它,你把它叫做:

blit_image("resources/images/fondblanc.bmp",fenetre,0,0); 

該文件名是31個字符通過我的計算,並不會裝入15個字符。

我還添加了一些printf語句到你的代碼中,這將幫助你找出它崩潰的地方。 您應該繼續添加更多的printf語句,直到您縮小崩潰。

+1

沒有printf調試。使用像gdb和valgrind這樣的真實工具。 –

+0

我用15代替了50,我重新編譯了代碼,現在它顯示我 temp是NULL 分段錯誤(核心轉儲) 所以問題必須在url?:( – sanaEsprit

+1

如果'SDL_LoadBMP(chemin);'返回NULL ,你應該閱讀文檔,並仔細檢查路徑,並***調查***。 – abelenky

0

很多您代碼中的問題是:

int blit_image(char chemin[15],SDL_Surface *fenetre,int posx,int posy) 
{ 
    [...] 
} 

該函數聲明返回int

您是否在該函數中看到return聲明?
它如何返回沒有返回語句的int

您的main函數被聲明爲返回int,並且令人驚訝的是,以return 0;結尾。
你如何在main中得到正確的結果,而不是在你的其他功能?

+0

雖然這絕對是一個應該解決的問題,但blit_image()的返回值永遠不會被捕獲,從而減輕了這種特殊的疏漏,並且使得它不太可能成爲分段錯誤的原因... – twalberg