gpt4 book ai didi

c - 为什么在编译我的代码 C (linux) 时出现段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 21:48:43 25 4
gpt4 key购买 nike

这是我的代码。
谁能帮我吗?

#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 编译它

代码编译成功,但输入 ./prog执行它向我显示的代码 "Segmentation fault (core dumped)"

但是所使用的文件存在于目录中

最佳答案

您声明了该函数:

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 语句,直到缩小崩溃范围。

关于c - 为什么在编译我的代码 C (linux) 时出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21861494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com