请问这是怎么回事?(有关内存越界)

问题描述

请问这是怎么回事?(有关内存越界)

以下是代码,进入后选择某些功能会出现以下警告:
有没有能帮忙改一下的,谢谢。

include

include

include

typedef struct link
{
char number[100];
char bookname[100];
char author[100];
char publish[100];
char time[100];
float price;
char status[100];
struct link *next;
}link;

link *Cbook()
{
int n=0;
link *p,*q,*head;
FILE *fp;
fp=fopen("图书信息.txt","r+");
if(fp==NULL)
printf("没有找到文件,请检查……");
p=(link *)malloc(sizeof(link));
if(p==NULL)
printf("申请内存出错!!!n");
fscanf(fp,"%s%s%s%s%s",p->number,p->bookname,p->author,p->publish,p->time);
fscanf(fp,"%f",&p->price);
fscanf(fp,"%s",p->status);
while(feof(fp)==0)
{
n++;
if(n==1)
{
head=p;
}
else
{
q=p;
p=(link *)malloc(sizeof(link));
if(p==NULL)
printf("申请内存出错!!!n");
fscanf(fp,"%s%s%s%s%s",p->number,p->bookname,p->author,p->publish,p->time);
fscanf(fp,"%f",&p->price);
fscanf(fp,"%s",p->status);
q->next=p;
}
}
p->next=NULL;
fclose(fp);
printf("信息已录入!!!");
return head;
}

void mainmenu()
{
printf("n*****************************************n");
printf("n** 欢迎使用图书管理系统 n");
printf("n
1.进入系统 0.退出系统 n");
printf("n
***************************************n");
printf("n 请选择");
}

void menu1()
{
printf("n*****************************************n");
printf("n** 1.进入图书查询系统 n");
printf("n
2.进入图书借还系统 n");
printf("n
3.保存所有图书信息 n");
printf("n
4.返回上级菜单 n");
printf("n
***************************************n");
printf("n请选择");
}

void cxmenu()
{
printf("n*****************************************n");
printf("n** 1.显示所有图书信息 n");
printf("n
2.按编号查询图书 n");
printf("n
3.按书名查询图书 n");
printf("n
4.按作者查询图书 n");
printf("n
5.返回上级菜单 n");
printf("n
**************************************n");
printf("n请选择");
}

void jhmenu()
{
printf("n*****************************************n");
printf("n** 1.借书 n");
printf("n
2.还书 n");
printf("n
3.返回上级菜单 n");
printf("n
***************************************n");
printf("n请选择");

}

void print(link head)
{
link *p;
p=head;
printf("n");
printf("n
*************************************************n");
printf("n编号tt书名tt作者tt出版社tt出版时间tt价格tt状态n");
while(p!=NULL)
{

printf("n");
printf("%st%st%st%st%s",p->number,p->bookname,p->author,p->publish,p->time);
printf("tt%.2f",p->price);
printf("tt%s",p->status);
printf("n");
p=p->next;
}
}

void hold(link *head)
{
link *p;
FILE *fp;
fp=fopen("图书信息.txt","w+");
if(fp==NULL)
{
printf("文件操作出错!!!");
exit(1);

}
p=head;
for(;p!=NULL;p=p->next)
{

fprintf(fp,"%st%st%st%st%s",p->number,p->bookname,p->author,p->publish,p->time);
fprintf(fp,"tt%.2f",p->price);
fprintf(fp,"tt%s",p->status);
fprintf(fp,"n");

}
fclose(fp);
printf("信息已保存。");
}

void holdcx(link t)
{
link *p=NULL;
FILE *fp;
fp=fopen("查询.txt","a+");
p=t;
fprintf(fp,"查询到的信息为:n");
fprintf(fp,"n");
fprintf(fp,"n
*************************************************n");
fprintf(fp,"n编号tt书名tt作者tt出版社tt出版时间tt价格tt状态n");
fprintf(fp,"n");
fprintf(fp,"%st%st%st%st%s",p->number,p->bookname,p->author,p->publish,p->time);
fprintf(fp,"tt%.2f",p->price);
fprintf(fp,"tt%s",p->status);
fprintf(fp,"n");
fclose(fp);
printf("信息已保存!!!");
}

link *findnumber(link *head)
{
char key[100];
link *h,*t1=NULL;
printf("请输入图书的编号:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->number)==0)
{
t1=h;
break;
}
}
return t1;
}

link *findboookname(link *head)
{
char key[100];
link *h,*t2=NULL;
printf("请输入图书的书名:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->bookname)==0)
{
t2=h;
break;
}
}
return t2;
}

link *findauthor(link *head)
{
char key[100];
link *h,*t3=NULL;
printf("请输入作者姓名:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->author)==0)
{
t3=h;
break;
}
}
return t3;
}

link *sortnumber(link *head)
{
link *p,*q,*temp;
temp=(link *)malloc(sizeof(link));
if(temp==NULL)
{
printf("申请内存出错!!!");
exit(1);
}
for(p=head;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(p->number,q->number)>0)
{
strcpy(temp->number,p->number);
strcpy(temp->bookname,p->bookname);
strcpy(temp->author,p->author);
strcpy(temp->publish,p->publish);
strcpy(temp->time,p->time);
temp->price=p->price;
strcpy(temp->status,p->status);

         strcpy(p->number,q->number);
         strcpy(p->bookname,q->bookname);
         strcpy(p->author,q->author);
         strcpy(p->publish,q->publish);
         strcpy(p->time,q->time);
         p->price=q->price;
         strcpy(p->status,temp->status); 

         strcpy(q->number,temp->number);
         strcpy(q->bookname,temp->bookname);
         strcpy(q->author,temp->author);
         strcpy(q->publish,temp->publish);
         strcpy(q->time,temp->time);
         q->price=temp->price;
         strcpy(q->status,temp->status);
      }
      }
  }

return head;
}

link borrowbook(link *head)
{
link *h,*p;
char ch,ch1[100]="在库",ch2[100]="不在库";
h=head;
printf("n
*************************************************n");
printf("tt欢迎使用借书系统!!!n");
p=findboookname(h);
if(p==NULL)
printf("借书失败,书库中没有这本书!!!");
else if(strcmp(p->status,ch2)==0)
printf("借书失败,图书不在库!!!");
else
{
printf("确认借出?Y/N:");
scanf("%c",&ch);
getchar();
if(ch=='Y'||ch=='y')
{

strcpy(p->status,ch2);
printf("借书成功,请返回上级菜单保存信息!!!");
}
if(ch=='N'||ch=='n')
{
printf("你没有借任何书!!!");
}
}
return head;
}

link returnbook(link *head)
{
link *h,*p;
char ch,ch1[100]="在库",ch2[100]="不在库";
h=head;
printf("n
*************************************************n");
printf("tt欢迎使用还书系统!!!n");
p=findboookname(h);
if(p==NULL)
printf("还书失败,这不是本书库的书!!");
else
{
printf("确认还书?Y/N:");
scanf("%c",&ch);
getchar();
if(ch=='Y'||ch=='y')
{

strcpy(p->status,ch1);
printf("还书成功,请返回上级菜单保存信息!!!");
}
if(ch=='N'||ch=='n')
{
printf("你没有还书!!!");
}
}
return head;
}

void main()
{
int a,b,c,d;
link h,*t;
L:system("cls");
mainmenu();
while(1)
{
scanf("%d",&a);
switch(a)
{
case 1:
{
B: system("cls");
menu1();
while(1)
{
scanf("%d",&b);
switch(b)
{
case 1:
{
system("cls");
cxmenu();
while(1)
{
scanf("%d",&c);
switch(c)
{
case 1:
{
print(h);
}break;
case 2:
{
t=findnumber(h);
if(t==NULL)
printf("没有要查询的图书!!!");
if(t!=NULL)
{
printf("查询的图书为:");
printf("n");
printf("n
*************************************************n");
printf("n编号tt书名tt作者tt出版社tt出版时间tt价格tt状态n");

            printf("n");
printf("%st%st%st%st%s",t->number,t->bookname,t->author,t->publish,t->time);
            printf("tt%.2f",t->price);
            printf("tt%s",t->status);
            printf("n");
           }
              }break;
           case 3:
           {
           t=findboookname(h);
           if(t==NULL)
           printf("没有要查询的图书!!!");
           if(t!=NULL)
           {
            printf("查询的图书为:");
            printf("n");

printf("n**************************************************n");
printf("n编号tt书名tt作者tt出版社tt出版时间tt价格tt状态n");
printf("n");
printf("%st%st%st%st%s",t->number,t->bookname,t->author,t->publish,t->time);
printf("tt%.2f",t->price);
printf("tt%s",t->status);
printf("n");
}
}break;
case 4:
{
t=findauthor(h);
if(t==NULL)
printf("没有要查询的图书!!!");
if(t!=NULL)
{
printf("查询的图书为:");
printf("n");
printf("n**************************************************n");
printf("n编号tt书名tt作者tt出版社tt出版时间tt价格tt状态n");
printf("n");
printf("%st%st%st%st%s",t->number,t->bookname,t->author,t->publish,t->time);
printf("tt%.2f",t->price);
printf("tt%s",t->status);
printf("n");
}
}
break;
case 5:
{
goto B;
}break;
}
}
}break;
case 2:
{
system("cls");
jhmenu();
while(1)
{
scanf("%d",&d);
switch(d)
{
case 1:
{
borrowbook(h);
}break;
case 2:
{
returnbook(h);
}break;
case 3:
{
goto B;
}break;
}
}
}break;
case 3:
{
hold(h);
}break;
case 4:
{
goto L;
}break;

}

}
}break;
case 0:
{
printf("ttt感谢使用,再见!!!");
exit(0);
}
}
}
}

解决方案

代码不全,建议你贴在 codepad.org 上,然后把链接写在这里。便于调试。

0xcccccccc 很典型是你的指针没有初始化造成的。

解决方案二:

有个关于C++的笑话,说的是“烫烫烫烫”

如果你不知道,你可以google 烫烫烫烫

因为烫的内码就是0xcccc,VC++调试版会把堆上的内存全部初始化成0xcc

解决方案三:

解决方案四:

设置断点,单步调试一下,找到具体出错的位置,一般都是指针,数组越界等

时间: 2024-08-01 20:07:14

请问这是怎么回事?(有关内存越界)的相关文章

C++堆内存越界问题调试

问题描述 C++堆内存越界问题调试 最近公司代码老是遇到glibc 断言死机,基本都是另外地方把这边内存踩了,另外地方的内存可能已经被覆盖或者释放后重用,这种情况要怎么查才能还原第一现场?死机一般都只有core文件和日志文件.我们用的是arm或x86嵌入式linux系统,内存一般1GB. 盼高手回复,万分感激!!! 解决方案 补充:嵌入式下用不了valgrind 解决方案二: 这种错误一般是你已有的两种数据结合处理.一个是core dump.用gdb等查看分析调用堆栈.找到出错的地方.如果不能直

C++内存越界问题及解决方法

与内存泄露相比,C++最令人头痛的问题是内存越界,而内存越界很多情况下是由于悬挂指针引起的. 假设一个指针变量: Object * ptr; 使用ptr时,我们除了要判断ptr是否为0以外,还要怀疑它指向的对象是否有效,是不是已经在别的地方被销毁了.我们希望当它指向的对象被销毁时,ptr被自动置为0. 显然,C++没有这种机制,但是,可以借助于boost::weak_ptr做到这一点. inline void null_deleter(void const *) { } class X { pr

android studio 报错,请问这是怎么回事?怎么解决

问题描述 android studio 报错,请问这是怎么回事?怎么解决 Error:warning: Ignoring InnerClasses attribute for an anonymous inner class Error:(com.qihoo.sdk.report.b) that doesn't come with an Error:associated EnclosingMethod attribute. This class was probably produced by

请问有谁知道windows系统共享内存映射方式通信时双方等待超时是什么原因

问题描述 请问有谁知道windows系统共享内存映射方式通信时双方等待超时是什么原因 //让服务器处理请求 if(!(::SetEvent(hExec))) { printf("发送请求失败!n"); bResult=false; return bResult; } //等待服务器处理完毕 long wait=::WaitForSingleObject(hDone, 5000); 解决方案 ::WaitForSingleObject(hDone, 5000); 等待5秒,(5000毫秒

我在开发板下insmod 加载驱动模块时直接显示killed了,请问这个是什么回事??

问题描述 我在开发板下insmod 加载驱动模块时直接显示killed了,请问这个是什么回事?? 大神们好!我在开发板下insmod 加载驱动模块时显示killed,请问这个是怎么回事呢?请教!!!

使用PageHeap.EXE或GFlags.EXE检查内存越界错误

必先利其器之一:使用PageHeap.EXE或GFlags.EXE检查内存越界错误 Article last modified on 2002-6-3 ---------------------------------------------------------------- The information in this article applies to: -        Microsoft Visual C++, 32-bit Editions, version 6.0, SP5

.net 2005的树控件在有的IE里可以打开,在有的点击的时候就展不开?请问各位是怎么回事啊?

问题描述 .net2005的树控件在有的IE里可以打开,在有的点击的时候就展不开?请问各位是怎么回事啊? 解决方案 解决方案二:各位大师,给点建议啊:)解决方案三:ding解决方案四:你能够告诉我是什么浏览器上点击打不开吗?我好像还从来没有遇见过阿

数据库连接问题:请问这是怎么回事?

问题描述 在连接SQL2005时,出现这样的错误:System.Data.SqlClient.SqlException:Aconnectionwassuccessfullyestablishedwiththeserver,butthenanerroroccuredduringtheloginprocess(provider:SharedMemoryProvider,error:0-管道的另一端上无任何进程.)我的连接字符串是:<appSettings><addkey="strC

在C#.net1.1中调用api kernel32.dll 在win xp上运行出现 没有注册类别 请问大侠是怎么回事??

问题描述 程序运行时抛出异常:Seetheendofthismessagefordetailsoninvokingjust-in-time(JIT)debugginginsteadofthisdialogbox.**************ExceptionText**************System.Runtime.InteropServices.COMException(0x80040154):没有注册类别atSystem.Windows.Forms.AxHost.CreateInsta