| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖
标题:找个大神 帮忙看看
只看楼主 加入收藏
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:6 
找个大神 帮忙看看
#incldue
using namespace std;
struct teacher{
char* name;
char* fangxiang;
int cs;
};
void output(struct teacher * th)
{
    cout<name<<":"<fangxiang<cs<<ENDL;
}
int main()
{
    int n,i;
struct teacher * th;
cout<<"please input the number:"<<ENDL;
cin>>n;
th=new struct teacher[n];
if(th==NULL)
{
    cout<<"memory creat false!!"<<ENDL;
}
else
{
    (th+i)->name=new char[n];
    (th+i)->fangxiang=new char[n];
    if((th+i)->name==NULL&&(th+i)->fangxiang==NULL)
{
    cout<<"memory creat false!"<<ENDL;
}
}
for(i=0;i<n;i++)
<N;I++)
{
    cout<<"please input the data"<<ENDL;
    cin>>(th+i)->name;
    cin>>(th+i)->fangxiang;
    cin>>(th+i)->cs;
}
for(i=0;i<n;i++)
<N;I++)
{
    output(th);
}
for(i=0;i<n;i++)
{
    delete[] (th+i)->fangxiang;
    (th+i)->fangxiang=NULL;
    delete[] (th+i)->name;
    (th+i)->name=NULL;   
}
delete[] th;
th=NULL;
return 0;
}
编译没错误  运行后出现内存问题 不知道怎么解决 求大神 指点;
搜索更多相关主题的帖子: please memory teacher false 
2013-03-04 22:38
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
收藏
得分:0 
上一个程序 写的时候 有错误 从新写了一个:
#include<iostream>
using namespace std;
struct teacher{
char* name;
char* fangxiang;
int cs;
};
void output(struct teacher * th)
{
    cout<<th->name<<":"<<th->fangxiang<<th->cs<<endl;}
int main()
{
    int n,i;
struct teacher * th;
cout<<"please input the number:";
cin>>n;
th=new struct teacher[n];
if(th==NULL)
{
    cout<<"memory creat false!!"<<endl;}
else
{
    (th+i)->name=new char[n];
    (th+i)->fangxiang=new char[n];
    if((th+i)->name==NULL&&(th+i)->fangxiang==NULL)
{
    cout<<"memory creat false!"<<endl;}
}
for(i=0;i<n;i++)
{
    cout<<"please input the data"<<endl;
    cin>>(th+i)->name;
    cin>>(th+i)->fangxiang;
    cin>>(th+i)->cs;
}
for(i=0;i<n;i++)
{
    output(th);
}
for(i=0;i<n;i++)
{
    delete[] (th+i)->fangxiang;
    (th+i)->fangxiang=NULL;
    delete[] (th+i)->name;
    (th+i)->name=NULL;   
}
delete[] th;
th=NULL;
return 0;
}
2013-03-04 22:46
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:10 
#include <iostream>
 using namespace std;
 struct teacher{
 char* name;
 char* fangxiang;
 int cs;
 };
 void output(struct teacher * th)
 {
     cout<<th->name<<":"<<th->fangxiang<<th->cs<<endl;
 }
 int main()
 {
     int n,i;
 struct teacher * th;
 cout<<"please input the number:"<<endl;
 cin>>n;
 th=new struct teacher[n];
 if(th==NULL)
 {
     cout<<"memory creat false!!"<<endl;
 }
 else
 {
     (th+i)->name=new char[n];
     (th+i)->fangxiang=new char[n];
     if((th+i)->name==NULL&&(th+i)->fangxiang==NULL)
 {
     cout<<"memory creat false!"<<endl;
 }
 }
 for(i=0;i<n;i++)
 {
     cout<<"please input the data"<<endl;
     cin>>(th+i)->name;
     cin>>(th+i)->fangxiang;
     cin>>(th+i)->cs;
 }
 for(i=0;i<n;i++)
 {
     output(th+i);
 }
 for(i=0;i<n;i++)
 {
     delete[] (th+i)->fangxiang;
     (th+i)->fangxiang=NULL;
     delete[] (th+i)->name;
     (th+i)->name=NULL;   
}
 delete[] th;
 th=NULL;
 return 0;
 }
我感觉你的程序有问题,虽然你是想存储很多教师的信息然后进行输出,个别的代码我都改了,编译也没什么错误了。为什么不能运行,问题大概处在用new开辟内存空间的表达上,new运算符使用的一般格式:new 类型 [初值],用new分配给数组空间时不能指定初值,程序不能运行估计内存不足等原因无法正常分配。真不好意思,我要睡觉了,没帮到你,给你个网址链接,你看看可有用http://

Maybe
2013-03-04 23:12
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:5 
test.cpp(23) : warning C4700: local variable 'i' used without having been initialized


[fly]存在即是合理[/fly]
2013-03-04 23:23
wsws23
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:105
专家分:107
注 册:2012-3-13
收藏
得分:0 
回复 3楼 邓士林
非常感谢 我也知道是内存的非配问题 只是不知道在哪  
内存不够 那不太可能吧
2013-03-04 23:31
joke5878
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-2-15
收藏
得分:0 
不懂路过的
2013-03-04 23:40
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:5 
回复 5楼 wsws23
程序在运行的时候用malloc或new申请任意多少的内存,程序员自己负责在何时用free或delete释放内存。动态内存的生存期由我们决定,使用非常灵活,但问题也最多。

Maybe
2013-03-05 22:39
快速回复:找个大神 帮忙看看
数据加载中...
 
   



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

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