关于new和delete的问题

五号智能 2013-12-25 09:34:30
在运行时,会出现heap corruption detected :after normal block错误 搞了一夜,也没弄明白是怎么回事,


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#define SONG_LENGTH 20
#define ARTIST_LENGTH 20
#define CD_LENGTH 20

class Song{
public:
char* song;
char* artist;
char* cd;
int rating;
Song* next;
};

Song* new_song(char* song, char* artist, char* cd, int rating){
Song *head=new Song;
head->song = new char(SONG_LENGTH+10);
head->artist = new char(ARTIST_LENGTH+10);
head->cd = new char(CD_LENGTH+10);

strcpy(head->song,song);
strcpy(head->artist,artist);
strcpy(head->cd,cd);
head->rating=rating;
head->next=NULL;

return head;
}
Song *insert_by_song(Song *head,Song *node)//插入
{
if(head==NULL)
{
node->next=NULL;
head=node;
return head;
}
else
{
node->next=head;
head=node;
return head;
}
}
Song* delete_odd(Song* head){
Song *p=head;
Song *before = NULL;
while(p!=NULL){
cout<<strlen(p->artist)<<endl;
system("pause");
if (p->rating%2==0){

if(before==NULL){
head=p->next;
delete [] (p->artist); //在运行时会出现heap corruption detected
delete [] (p->cd);
delete [] (p->song);
delete p;
p=head;

}
else{
delete [](p->artist);
delete [](p->cd);
delete [](p->song);
p->artist=NULL;
p->cd=NULL;
p->song=NULL;
before->next=p->next;
delete p;
p=before->next;
}
}
else{
before=p;
p=p->next;
}

}
return head;
}
int main(int argc, char** argv){
cout<<"The size of the struct type Song is "
<<sizeof(Song)
<<" bytes."
<<endl;
if(argc<2)
{
cout<<"Error: Insufficient CommandLine arguments.\n";
cout<<"Usage: linkedlist <filename><CR>\n";
return 0;
}
Song *head = NULL;
print_list(head);
Song *node = NULL;
int rating; /* cout<< hex <<uppercase;*/
ifstream source(*(argv+1));

while(!source.eof())
{

char *song = new char(SONG_LENGTH);
char *artist = new char(ARTIST_LENGTH);
char *cd = new char(CD_LENGTH);
source >> song; //从文件输入
source >> artist;
source >> cd;
source >> rating;


new_song(song,artist,cd,rating); //创建新的结构
head = insert_by_song(head,node); //插入链表
cout << endl;
}
source.close();

delete_odd(head);

system("pause");
return 0;
}

下面是要读取的TXT文件内容

//荷塘月色 凤凰传奇 未知/Unknown 4
//光辉岁月 Beyond 未知/Unknown 3
//天机 张学友&白鹭 影视歌曲 4
//Yesterday Beatles Help! 5
//Paradise Cody_Simpson <Paradise> 3
//Misery Maroon_5 Hands_All_Over 4
//Smooth Santana Supernatural 5
//Something Beatles Abbey_Road 5
//GoodtimeGirl Grant_Grieves 45S 4
//Sunshine Minnutes The_Big_C 2
//Time E.J.Bucher SongInGoodTime 3
//去伊犁的路上 刀郎 未知/Unknown 5
//葫芦娃 未知/Unknown 儿童歌曲 4
//恰似你的温柔 邓丽君 经典歌曲 5
//You_And_Me Shane_Filan 奥运 5
//外婆的澎湖湾 蔡琴 爱像一首歌 5


...全文
131 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
五号智能 2013-12-26
  • 打赏
  • 举报
回复
这还真不是我写的 我就没学过C,我写的东西基本上大部分功能全是封装的.我头一回看到这程序的时候,也是觉得很蛋疼的.
大奶兔白糖 2013-12-25
  • 打赏
  • 举报
回复
另外,楼主,new和delete配对使用,new[]和delete[]配对使用。楼主用new分配空间,用delete[]释放,肯定会出问题。
大奶兔白糖 2013-12-25
  • 打赏
  • 举报
回复
引用 1 楼 thefirstz 的回复:
head->song = new char(SONG_LENGTH+10); head->artist = new char(ARTIST_LENGTH+10); head->cd = new char(CD_LENGTH+10); 这里的圆括号都应该换成中括号吧,一个是初始化,一个是分配数组
正解,这样子使用的话都溢出了。。。
zhuobattle 2013-12-25
  • 打赏
  • 举报
回复
宏定义的长度不够,你某些歌的歌曲名长度已经超过20个字节了。
昵称很不好取 2013-12-25
  • 打赏
  • 举报
回复
head->song = new char(SONG_LENGTH+10); head->artist = new char(ARTIST_LENGTH+10); head->cd = new char(CD_LENGTH+10); 这里的圆括号都应该换成中括号吧,一个是初始化,一个是分配数组
vipcxj 2013-12-25
  • 打赏
  • 举报
回复
LZ,你用C风格写C++代码,好蛋疼啊~

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧