请问这篇代码有什么问题吗
在编译器中确实能正常运行但是会警告
1、取消对 NULL 指针的引用。“L->data”包含与“realloc()`86\148”相同的 NULL 值。
2、“realloc”可能返回 null 指针: 将 null 指针赋给“L->data”(后者将作为参数传递给“realloc”)将导致原始内存块泄漏。
3、使用未初始化的内存“*L->data”。
4、写入 "L->data" 时缓冲区溢出。
```c
#include
#include
#define INITSIZE 10
//类型定义
typedef int ElemType;
typedef struct
{
ElemType* data;
int listSize,lenth;
}SqList;
//函数原型
void report(void);
void initList(SqList* L);
void creatList(SqList* L);
int getLen(SqList* L);
void listList(SqList* L);
void getElem(SqList* L, int sn, ElemType* e);
int locate(SqList* L, ElemType e);
int ins(SqList* L, int sn, ElemType e);
//主函数
int main(void)
{
SqList L;
initList(&L);
printf("顺序表测试\n\n");
creatList(&L);
int sn; ElemType e;
{
printf("取元素测试\n输入所取元素的位置序号:");
scanf_s("%d", &sn);
getElem(&L, sn, &e);
printf("序号%d位置上的元素为%d", sn, e);
}
{
printf("元素定位测试\n输入想要定位的元素:");
scanf_s("%d", &e);
sn = locate(&L, e);
if (sn)
printf("%d的位置序号为%d", e, sn);
else
printf("没有该元素!");
}
{
printf("元素插入测试\n请输入要插入的元素:");
scanf_s("%d", &e);
printf("请输入插入元素的序号位置:");
scanf_s("%d", &sn);
if (ins(&L, sn, e))
listList(&L);
}
return 0;
}
//报错并退出
void report(void)
{
printf("内存申请错误!");
exit(1);
}
//顺序表的初始化
void initList(SqList* L)
{
L->data = (ElemType*)malloc(INITSIZE * sizeof(ElemType));
if (!L->data)
report();
L->lenth = 0;
L->listSize = INITSIZE;
}
//建表
void creatList(SqList* L)
{
ElemType x;
printf("输入存入数据表中的数据,以0结束:\n");
scanf_s("%d", &x);
while (x)
{
if (L->lenth == L->listSize)
{
L->data = (ElemType*)realloc(L->data, (++L->listSize) * sizeof(ElemType));
if (!L->data)
report();
}
L->data[L->lenth++] = x;
scanf_s("%d", &x);
}
listList(L);
}
//求表长
int getLen(SqList *L)
{
return (L->lenth);
}
//求表长
void listList(SqList* L)
{
if (getLen(L))
{
printf("顺序表元素数量为:%d,分别为:\n", getLen(L));
for (int i = 0; i < L->lenth; i++)
printf("%d ", L->data[i]);
printf("\n");
}
else
{
printf("顺序表为空,请重新");
creatList(L);
}
}
//取元素
void getElem(SqList* L, int sn, ElemType* e)
{
if (sn >= 1 && sn < L->lenth+1)
*e = L->data[--sn];
else printf("位置非法!");
}
//定位
int locate(SqList* L, ElemType e)
{
int i = 0;
while (i <= L->lenth )
{
if(L->data[i++]==e)
return i;
}
return 0;
}
//插入
int ins(SqList* L, int sn, ElemType e)
{
if (sn < 1 || sn > L->lenth+1)
{
printf("位置非法!");
return 0;
}
if (L->lenth == L->listSize)
{
L->data = (ElemType*)realloc(L->data, (++L->listSize) * sizeof(ElemType));
if (!L->data)
report();
}
for(int i=L->lenth-1;i>=sn-1;i--)
{
L->data[i+1] = L->data[i];
}
L->data[sn - 1] = e;
L->lenth++;
printf("插入成功!");
return 1;
}
```
不知道你这个问题是否已经解决, 如果还没有解决的话: