当前位置: 技术问答>linux和unix
linux C编程出了“段错误” !!!
来源: 互联网 发布时间:2016-08-21
本文导语: 一个线性链表的初始化程序,运行时显示“Segmentation fault" ,代码如下: # include # include # include # include # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define OVERFLOW -1 struct sqlist { ...
一个线性链表的初始化程序,运行时显示“Segmentation fault" ,代码如下:
# include
# include
# include
# include
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW -1
struct sqlist
{
int * p;
int length;
int listsize;
}sqlist;
int Initlist_sq(struct sqlist * L, int LIST_INIT_SIZE)
{
L->p = (int * )malloc(LIST_INIT_SIZE * sizeof(int));
if ( L->p== NULL ) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
void main()
{
struct sqlist * p;
int status = 0;
status = Initlist_sq(p, 100);
if( status)
printf("memory allocation succeedn");
else
printf("memory allocation failedn");
}
# include
# include
# include
# include
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define OVERFLOW -1
struct sqlist
{
int * p;
int length;
int listsize;
}sqlist;
int Initlist_sq(struct sqlist * L, int LIST_INIT_SIZE)
{
L->p = (int * )malloc(LIST_INIT_SIZE * sizeof(int));
if ( L->p== NULL ) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
void main()
{
struct sqlist * p;
int status = 0;
status = Initlist_sq(p, 100);
if( status)
printf("memory allocation succeedn");
else
printf("memory allocation failedn");
}
|
int Initlist_sq(struct sqlist * L, int LIST_INIT_SIZE)
{
L->p = (int * )malloc(LIST_INIT_SIZE * sizeof(int));//使用无效指针,你应该给L也分配内存
if ( L->p== NULL ) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
void main()
{
struct sqlist * p;//是无效指针
int status = 0;
status = Initlist_sq(p, 100);
if( status)
printf("memory allocation succeedn");
else
printf("memory allocation failedn");
}
{
L->p = (int * )malloc(LIST_INIT_SIZE * sizeof(int));//使用无效指针,你应该给L也分配内存
if ( L->p== NULL ) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
void main()
{
struct sqlist * p;//是无效指针
int status = 0;
status = Initlist_sq(p, 100);
if( status)
printf("memory allocation succeedn");
else
printf("memory allocation failedn");
}