当前位置: 技术问答>linux和unix
用xlib.h编的程序,运行时总显示段错误,望指点!
来源: 互联网 发布时间:2016-09-14
本文导语: 用xlib.h编的程序,运行时总显示段错误,经过调试,问题出在fread函数这里,我看了几天都没有结果,还希望大家看看,另外代码有什么可以优化的地方,也请指教。 谢谢! #include #include #include struct TIME {int day...
用xlib.h编的程序,运行时总显示段错误,经过调试,问题出在fread函数这里,我看了几天都没有结果,还希望大家看看,另外代码有什么可以优化的地方,也请指教。
谢谢!
#include
#include
#include
struct TIME
{int day;
int hour;
int minute;
int second;
};
struct item
{char outer[20];
int ocategory;
char receiver[20];
int rcategory;
struct TIME time;
struct item *next;
};
void DRAW(Display *display,Window win,GC gc,struct item *p)
{XDrawLine(display,win,gc,20,20,40,100);
}
struct item *read(FILE *fp)
{struct item *p,*p1,*head;
p=(struct item*)malloc(sizeof(struct item));
head=p;
if(!p){printf("no enough memory!");exit(0);};
while(fread(p,sizeof(struct item),1,fp))
{ p1=p;
p=(struct item*)malloc(sizeof(struct item));
if(!p){printf("no enough memory!");exit(0);};
p1->next=p;
};
p1->next =NULL;
free(p);
return head;
}
int main(int argc, char *argv[])
{struct item *head,*p;
FILE *fp;
Display *display;
XEvent e;
if((display=XOpenDisplay(NULL))==NULL)
{printf("Can't connect to X severn");
exit(-1);}
GC gc;
XGCValues values;
if(!(fp=fopen("resource.r","r"))){printf("can't open this file.n");exit(0);}
fseek(fp,0,SEEK_END);
if(ftell==0){printf("no content!n");exit(0);};
fseek(fp,0,SEEK_SET);
head=read(fp);
fclose(fp);
int s=DefaultScreen(display);
int screen_width=DisplayWidth(display,s)/2;
int screen_height=DisplayHeight(display,s)/2;
Window win;
win=XCreateSimpleWindow(display,RootWindow(display,s),10,10,screen_width,screen_height,2,BlackPixel(display,s),WhitePixel(display,s));
gc=XCreateGC(display,win,0,&values);
if(gc==NULL)
{printf("Can't create GC");
exit(-1);
}
DRAW(display,win,gc,p);
XSelectInput(display,win,ExposureMask|KeyPressMask);
XMapWindow(display,win);
while(1)
{XNextEvent(display,&e);
if(e.type==Expose)
{DRAW(display,win,gc,p);
}
if(e.type==KeyPress)
break;
}
XCloseDisplay(display);
return 0;
}
谢谢!
#include
#include
#include
struct TIME
{int day;
int hour;
int minute;
int second;
};
struct item
{char outer[20];
int ocategory;
char receiver[20];
int rcategory;
struct TIME time;
struct item *next;
};
void DRAW(Display *display,Window win,GC gc,struct item *p)
{XDrawLine(display,win,gc,20,20,40,100);
}
struct item *read(FILE *fp)
{struct item *p,*p1,*head;
p=(struct item*)malloc(sizeof(struct item));
head=p;
if(!p){printf("no enough memory!");exit(0);};
while(fread(p,sizeof(struct item),1,fp))
{ p1=p;
p=(struct item*)malloc(sizeof(struct item));
if(!p){printf("no enough memory!");exit(0);};
p1->next=p;
};
p1->next =NULL;
free(p);
return head;
}
int main(int argc, char *argv[])
{struct item *head,*p;
FILE *fp;
Display *display;
XEvent e;
if((display=XOpenDisplay(NULL))==NULL)
{printf("Can't connect to X severn");
exit(-1);}
GC gc;
XGCValues values;
if(!(fp=fopen("resource.r","r"))){printf("can't open this file.n");exit(0);}
fseek(fp,0,SEEK_END);
if(ftell==0){printf("no content!n");exit(0);};
fseek(fp,0,SEEK_SET);
head=read(fp);
fclose(fp);
int s=DefaultScreen(display);
int screen_width=DisplayWidth(display,s)/2;
int screen_height=DisplayHeight(display,s)/2;
Window win;
win=XCreateSimpleWindow(display,RootWindow(display,s),10,10,screen_width,screen_height,2,BlackPixel(display,s),WhitePixel(display,s));
gc=XCreateGC(display,win,0,&values);
if(gc==NULL)
{printf("Can't create GC");
exit(-1);
}
DRAW(display,win,gc,p);
XSelectInput(display,win,ExposureMask|KeyPressMask);
XMapWindow(display,win);
while(1)
{XNextEvent(display,&e);
if(e.type==Expose)
{DRAW(display,win,gc,p);
}
if(e.type==KeyPress)
break;
}
XCloseDisplay(display);
return 0;
}
|
段错误一般是你的指针操作失误
单步调试一下
单步调试一下
|
linux下单步调试怎么进行? gdb命令
gdb命令怎么用? ——google之
gdb命令怎么用? ——google之
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。