| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 535 人关注过本帖
标题:怎么老是说访问非法内存?
只看楼主 加入收藏
红糖水
Rank: 2
等 级:论坛游民
帖 子:42
专家分:11
注 册:2013-2-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
怎么老是说访问非法内存?
各位高手给点提示。。。跪谢了!
这是我的代码,但系统老是说访问非法内存,而在VC上又能正常运行,这让我很纠结。。。
#include<iostream>
using namespace std;
int main()
{
    int *p,i,sum=0,*m,n;//声明变量和指针
    cout<<"Please input the number of the elements in the array:"<<endl;
    cin>>n;//输入一个整数
    p=new int();//开辟n个存储空间
    m=p;
    cout<<"Please input the elementes in the array:"<<endl;
    for(i=0;i<n;i++)//输入数据
    {
        cin>>*p;
        sum=sum+*p;//求和
        p++;
    }
    cout<<"The array you input:"<<endl;
    p=m;
    for(i=0;i<n;i++)//输出数据
    {
        cout<<*p;
        cout<<" ";
        p++;
    }
    cout<<endl;
    cout<<"The sum of the array is:"<<endl;
    cout<<sum<<endl;
    delete []p;//释放内存
    return(0);
}
搜索更多相关主题的帖子: 内存 namespace elements include number 
2013-03-17 08:11
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
程序代码:
#include<iostream>
using namespace std;
int main()
{
    int *p,i,sum=0,*m,n;//声明变量和指针
    cout<<"Please input the number of the elements in the array:"<<endl;
    cin>>n;//输入一个整数
    p=new int [n];//开辟n个存储空间
    m=p;
    cout<<"Please input the elementes in the array:"<<endl;
    for(i=0;i<n;i++)//输入数据
    {
        cin>>*p;
        sum=sum+*p;//求和
        p++;
    }
    cout<<"The array you input:"<<endl;
    p=m;
    for(i=0;i<n;i++)//输出数据
    {
        cout<<*p;
        cout<<" ";
        p++;
    }
    cout<<endl;
    cout<<"The sum of the array is:"<<endl;
    cout<<sum<<endl;
    p=NULL;
    m=NULL;
    delete p;//释放内存
    delete m;
    return 0;
} 

c++ 不是很熟悉  小小的改动了一下  你试试看

DO IT YOURSELF !
2013-03-17 08:41
红糖水
Rank: 2
等 级:论坛游民
帖 子:42
专家分:11
注 册:2013-2-3
收藏
得分:0 
回复 2楼 wp231957
太感谢大牛了,程序正确了。。。学到很多
2013-03-17 20:14
快速回复:怎么老是说访问非法内存?
数据加载中...
 
   



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

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