| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1683 人关注过本帖
标题:哪个兄弟帮忙调试下,指针分配问题,0 error,0 warning
取消只看楼主 加入收藏
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
 问题点数:0 回复次数:6 
哪个兄弟帮忙调试下,指针分配问题,0 error,0 warning
程序代码:
#include <iostream>
#include <malloc.h>
using namespace std;
class Array
{
    int num;
    int *pt;
public:
    Array(int n,int *p)
    {
        num=n;
        pt=p=(int*)malloc(sizeof(int));//这个好像怪怪的,但没有又不行
    }
    void create();
    void show();
    void delSame();
    void showNum();
};


void Array::create()
{
    int x;
    int *p;
    p=pt;
    int n=num;
    cout<<"Please input the member of the array"<<endl;
    do
    {
        cin>>x;
        *p=x;
        p++;
        n--;
    }while(n);
}
void Array::show()
{
    int *p=pt,n=num;
    while(n)
    {
       n--;
       cout<<*p<<" ";
       p++;         
    }
    cout<<endl;
}

void Array::delSame()
{
    int *p=pt;
    for(int i=1;i<num;i++)
    {
       if(*(p+i-1)==*(p+i))
       {   
           for(int j=i;j<num-1;j++)
             *(p+j)=*(p+j+1);
           num--;
           i--;
       }

    }
}

void Array::showNum()
{    
    int *p=pt;
    int k=1;
    for(int i=0;i<num;i++)
    {    
        for(int j=i+1;j<num;j++)    
            if(*(p+i)==*(p+j))
                k++;
        cout<<k<<" ";
        i=i+k-1;
        k=1;
    }
    cout<<endl;
}
int main()
{
    int n;
    int *p;
    cout<<"Input the length of the array"<<endl;
    cin>>n;
    Array a(n,p);
    a.create();
    a.showNum();
    a.delSame();
    a.show();
    system("pause");
    return 0;
}


程序是删除一个升序数组中相同的项,然后还要统计相同项个数,要求用指针动态开辟,不是链表。我用devcpp都调试好了,都没问题,就是如果数字大于26的话,程序就会自动关闭,25或者更小就完全没问题。实在看不出哪里问题啊!估计是指针使用不当,麻烦哪位高手帮忙解释解释!刚学c++不久,以前接触过学过c,但是连入门都不算,指针很多的有问题。
搜索更多相关主题的帖子: warning 分配问题 指针 兄弟 
2008-09-08 20:49
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
版主大侠  帮帮忙啊
2008-09-09 22:02
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
可是题目要求要动态开辟空间啊
2008-09-10 21:12
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
问题似乎找到了 ,是p++的问题; 因为p++加到一定次数,他的地址是不确定的,可能是系统变量的地址,所以这样盲目的乱加肯定出问题的,因为计算机不止程序在用,还有操作系统也在用,操作系统怎么可能让程序乱改自己的东西。
2008-09-11 10:21
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
还是不对啊!
2008-09-13 13:02
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
我倒,兄弟你都没运行一下吗?一运行就自动关闭了啊;我自己改了改
2008-09-13 22:22
joker39
Rank: 1
等 级:新手上路
威 望:1
帖 子:78
专家分:0
注 册:2007-9-6
收藏
得分:0 
#include <iostream>
using namespace std;
class Array
{
    int num;
    int *pt;
public:
    Array(int n)
    {
        num=n;
        pt=new int[num];
    }
    void create();
    void show();
    void delSame();
    void showNum();
};


void Array::create()
{
    int x;
    int *p;
    p=pt;
    int n=num;
    int i=0;
    cout<<"Please input the member of the array"<<endl;
    do
    {
        cin>>x;
        p[i++]=x;
    }while(i<n);
}
void Array::show()
{
    int *p=pt;
    int i=0;
    int n=num;
    while(i<n)
    {
       cout<<p[i++]<<" ";        
    }
    cout<<endl;
}

void Array::delSame()
{
    int *p=pt;
    for(int i=1;i<num;i++)
    {
       if(p[i-1]==p[i])
       {   
           for(int j=i;j<num-1;j++)
             p[j]=p[j+1];
           num--;
           i--;
       }

    }
}

void Array::showNum()
{   
    int *p=pt;
    int k=1;
    for(int i=0;i<num;i++)
    {    
        for(int j=i+1;j<num;j++)    
            if(p[i]==p[j])
                k++;
        cout<<k<<" ";
        i=i+k-1;
        k=1;
    }
    cout<<endl;
}
int main()
{
    int n;
    cout<<"Input the length of the array"<<endl;
    cin>>n;
    Array a(n);
    a.create();
    a.showNum();
    a.delSame();
    a.show();
    system("pause");
    return 0;
}
2008-09-13 22:23
快速回复:哪个兄弟帮忙调试下,指针分配问题,0 error,0 warning
数据加载中...
 
   



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

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