| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 950 人关注过本帖
标题:数组越界?
只看楼主 加入收藏
nick_annie
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2005-11-19
收藏
 问题点数:0 回复次数:6 
数组越界?

#include<iostream>
using namespace std;
struct node
{
node()
{
next=NULL;
}

/**
node(int *y,char *s,int x,node *link)
{
for(int a=0,b=0;y[a]!=-1;a++,b++)
music[b]=y[a];
name=s;
i=x;
next=link;
}
/**/

int music[10];
char name[10]; // 改过
int i;
node *next;
};

class LinkList
{
public:
LinkList()
{
first=NULL;
n=0;
}
void Insert(int pos,int m[],char str[]);
void Setup();
void Display();
void Play(node *p);
int length()
{
return n;
}
node* SetPos(int pos);
protected:
node *first;
int n;
};

node* LinkList::SetPos(int pos)
{
node *q=first;
for(int i=0;i<pos;i++)
q=q->next;
return q;
}

void LinkList::Setup()
{
node *q,*p;
int xiaomifeng[]={1,2,3,4,5,-1};
// q=new node(xiaomifeng,"小蜜蜂",1,NULL);
q=new node;
q->i=1;
memcpy(q->music,xiaomifeng,sizeof(q->music));
strcpy(q->name,"小蜜蜂");
q->next=first;
first=q;

int yujian[]={5,4,3,2,1,-1};
p=new node;
p->i=2;
::memcpy(p->music,yujian,sizeof(p->music));
::strcpy(p->name,"遇见");
p->next=q->next;
q->next=p;

// p=new node(yujian,"遇见",2,NULL);
// p->next=q->next;
// q->next=p;

n+=2;
}

void LinkList::Play(node *p)
{
node *q=p;
int i=0;
while(q->music[i]!=-1)
{
cout<<q->music[i];
i++;
}

cout<<endl;
}


void LinkList::Insert(int pos,int m[],char str[])
{
node *p,*q;
p=new node;
p->i=pos+1;
::memcpy(p->music,m,sizeof(p->music));
::strcpy(p->name,str);
if(pos>0)
{
q=SetPos(pos-1);
p->next=q->next;
q->next=p;
}
else
{
p->next=first;
first=p;
}
n++;
}


void LinkList::Display()
{
node *m=first;
int j;
cout<<"歌曲列表\n";
for(int a=1;a<=n;a++)
{
cout<<a<<'.'<<m->name<<endl;
m=m->next;
}
cout<<"按-1可以退到上一级别菜单\n";
cout<<"请选择:";
cin>>j;
while(j>0)
{
j=j-1;
node *z=SetPos(j);
Play(z);
cout<<"歌曲列表\n";
node *f=first;
for(int a=1;a<=n;a++)
{
cout<<a<<'.'<<f->name<<endl;
f=f->next;
}
cout<<"按-1可以退到上一级别菜单\n";
cout<<"请选择:";
cin>>j;
}

}
int main()
{
LinkList nick;
int w,x,y,z=0;
int s[100];
char str[10];
nick.Setup();
cout<<"**********************************欢迎使用此软件********************************\n";
cout<<" 1.点播歌曲\n";
cout<<" 2.自己写歌\n";
cout<<" 3.退出系统\n\n\n";
cout<<" 请做出选择....:";
cin>>x;
while(x>0)
{
if(x==1)
nick.Display();
if(x==2)
{
cout<<"请输入这是第几首歌:";
cin>>w;
cout<<"请输入歌曲的音阶以-1结束:";
cin>>y;
while(y!=-1)
{
s[z]=y;
z++;
cin>>y;
}
s[++z]=-1;
cout<<"请输入歌名:";
cin>>str;
w=w-1;
nick.Insert(w,s,str);
}


if(x==3)
{
cout<<" 欢迎再次使用!\n";
break;
}
cout<<"**********************************欢迎使用此软件********************************\n";
cout<<" 1.点播歌曲\n";
cout<<" 2.自己写歌\n";
cout<<" 3.退出系统\n\n\n";
cout<<" 请做出选择....:";
cin>>x;
}
return 0;
}

程序编译没错误...运行时当写入数组进行输出时..出现乱码...请指教

搜索更多相关主题的帖子: 越界 
2006-07-02 02:50
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 
vc6.0
测试通过,没有什么乱码.

2006-07-02 08:21
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
应该不是,我刚才调试运行了下,发现在两个成员函数中间,地址内的内容发生了改变,本来应该是“小蜜蜂”的,结果在那个地址里都是“烫”

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-07-02 09:25
nick_annie
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2005-11-19
收藏
得分:0 
有的....当使用2自己写歌的时候先输入数组..然后返回上一极菜单..用点播歌曲..放你刚刚输入的数组时..输出的就是一堆乱码

2006-07-02 10:07
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 

LZ,你那不是输入数组
是输入数组里的一个元素,还不如把music数组改成整数
node* LinkList::SetPos(int pos)
{
node *q=first;
for(int i=0;i<pos-1;i++)
q=q->next;
return q;
}
while(j>0)
{
// j=j-1;
node *z=SetPos(j);
Play(z);
cout<<"歌曲列表\n";
node *f=first;

while(y!=-1)
{
s[z]=y;
z++;
cin>>y;
}
s[z]=-1;


楼主,我没看全你的代码(很乱,还没注释),根据你说我的,我认为你数组越界,随便-1结果就出来了
又发现输出的是上面的歌的内容,找到位置不让那个-1,好一点的结果又出来了,但有随机数,估计你判断结束那里有错
发现你已-1为结束,找到给-1的地方不让它++Z,结果就正确了........


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-07-03 10:37
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 

#include<iostream>
using namespace std;
struct node
{
node()
{
next=NULL;
}

/**
node(int *y,char *s,int x,node *link)
{
for(int a=0,b=0;y[a]!=-1;a++,b++)
music[b]=y[a];
name=s;
i=x;
next=link;
}
/**/

int music[10];
char name[10]; // 改过
int i;
node *next;
};

class LinkList
{
public:
LinkList()
{
first=NULL;
n=0;
}
void Insert(int pos,int m[],char str[]);
void Setup();
void Display();
void Play(node *p);
int length()
{
return n;
}
node* SetPos(int pos);
protected:
node *first;
int n;
};

node* LinkList::SetPos(int pos)
{
node *q=first;
for(int i=0;i<pos-1;i++)
q=q->next;
return q;
}

void LinkList::Setup()
{
node *q,*p;
int xiaomifeng[]={1,2,3,4,5,-1};
// q=new node(xiaomifeng,"小蜜蜂",1,NULL);
q=new node;
q->i=1;
memcpy(q->music,xiaomifeng,sizeof(q->music));
strcpy(q->name,"小蜜蜂");
q->next=first;
first=q;

int yujian[]={5,4,3,2,1,-1};
p=new node;
p->i=2;
::memcpy(p->music,yujian,sizeof(p->music));
::strcpy(p->name,"遇见");
p->next=q->next;
q->next=p;

// p=new node(yujian,"遇见",2,NULL);
// p->next=q->next;
// q->next=p;

n+=2;
}

void LinkList::Play(node *p)
{
node *q=p;
int i=0;
while(q->music[i]!=-1)
{
cout<<q->music[i];
i++;
}

cout<<endl;
}


void LinkList::Insert(int pos,int m[],char str[])
{
node *p,*q;
p=new node;
p->i=pos+1;
::memcpy(p->music,m,sizeof(p->music));
::strcpy(p->name,str);
if(pos>0)
{
q=SetPos(pos-1);
p->next=q->next;
q->next=p;
}
else
{
p->next=first;
first=p;
}
n++;
}


void LinkList::Display()
{
node *m=first;
int j;
cout<<"歌曲列表\n";
for(int a=1;a<=n;a++)
{
cout<<a<<'.'<<m->name<<endl;
m=m->next;
}
cout<<"按-1可以退到上一级别菜单\n";
cout<<"请选择:";
cin>>j;
while(j>0)
{
// j=j-1;
node *z=SetPos(j);
Play(z);
cout<<"歌曲列表\n";
node *f=first;
for(int a=1;a<=n;a++)
{
cout<<a<<'.'<<f->name<<endl;
f=f->next;
}
cout<<"按-1可以退到上一级别菜单\n";
cout<<"请选择:";
cin>>j;
}

}
int main()
{
LinkList nick;
int w,x,y,z=0;
int s[100];
char str[10];
nick.Setup();
cout<<"**********************************欢迎使用此软件********************************\n";
cout<<" 1.点播歌曲\n";
cout<<" 2.自己写歌\n";
cout<<" 3.退出系统\n\n\n";
cout<<" 请做出选择....:";
cin>>x;
while(x>0)
{
if(x==1)
nick.Display();
if(x==2)
{
cout<<"请输入这是第几首歌:";
cin>>w;
cout<<"请输入歌曲的音阶以-1结束:";
cin>>y;
while(y!=-1)
{
s[z]=y;
z++;
cin>>y;
}
s[z]=-1;
cout<<"请输入歌名:";
cin>>str;
w=w-1;
nick.Insert(w,s,str);
}


if(x==3)
{
cout<<" 欢迎再次使用!\n";
break;
}
cout<<"**********************************欢迎使用此软件********************************\n";
cout<<" 1.点播歌曲\n";
cout<<" 2.自己写歌\n";
cout<<" 3.退出系统\n\n\n";
cout<<" 请做出选择....:";
cin>>x;
}
return 0;
}
//正确的


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-07-03 10:38
nick_annie
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2005-11-19
收藏
得分:0 
谢~~~

2006-07-09 16:02
快速回复:数组越界?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012259 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved