注册 登录
编程论坛 C++教室

哪个兄弟帮忙调试下,指针分配问题,0 error,0 warning

joker39 发布于 2008-09-08 20:49, 1687 次点击
程序代码:
#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,但是连入门都不算,指针很多的有问题。
11 回复
#2
joker392008-09-09 22:02
版主大侠  帮帮忙啊
#3
blueboy820062008-09-10 13:06
第68行应该是:for(int i=0;i<num-1;i++) 吧
另外,觉得delSame()算法有问题...
研究中......
#4
守鹤2008-09-10 20:21
声明一个类时,不能使用动态分配,因为类是一个抽象数据类型。
正如:int  a[];
      a=new int[12];
#5
joker392008-09-10 21:12
可是题目要求要动态开辟空间啊
#6
joker392008-09-11 10:21
问题似乎找到了 ,是p++的问题; 因为p++加到一定次数,他的地址是不确定的,可能是系统变量的地址,所以这样盲目的乱加肯定出问题的,因为计算机不止程序在用,还有操作系统也在用,操作系统怎么可能让程序乱改自己的东西。
#7
insmile2008-09-12 10:49
内存泄漏,数组溢出
#8
dsj_22008-09-13 08:31
我对楼上这位兄弟的程序进行了改写!
#include<iostream>
#include<malloc.h>
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();

    ~Array()
    {
        delete []pt;

    }
};

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++)
    {
        for(int k=i;k<num;k++)
        {
                if(*(p+i-1)==*(p+k))
                {
                    for(int j=k;j<num-1;j++)
                        *(p+j)=*(p+j+1);

                    num--;
                    i--;
                }
        }
    }
}

void Array::showNum()
{

    int *p=pt;

    int k=1,m=0,n=0,flag=1;

    int *temp=new int[num/2];//辅助数组,用于检查是否已计过数

    for(int i=0;i<num;i++)
    {
        for(int j=i+1;j<num;j++)
            if(*(p+i)==*(p+j))
                k++;
        if(k>1)
        {
            

            for(n=0;n<m;n++)
            {
                if(*(p+i)==temp[n])
                    flag=2;
                
            }
            
            if(flag==1)
            cout<<*(p+i)<<"的个数为:"<<k<<" "<<endl;

            flag=1;

            temp[m++]=*(p+i);
        }

    //    i=i+k-1;

        k=1;

    }

    cout<<endl;
}


void main()
{
    int n;
//    int *p;

    cout<<"Input the length of the array"<<endl;

    cin>>n;

    Array a(n);

    a.create();
    a.show();

    a.showNum();

    a.delSame();

    a.show();




}
#9
joker392008-09-13 13:02
还是不对啊!
#10
PeterOffice2008-09-13 15:02
修改之后的,看看还有什么问题
#include <iostream>
#include <malloc.h>

using namespace std;
class Array
{
    int num;
    int *pt;
public:
    Array(int n)
    {
        num=n;
       // pt=p=(int*)malloc(sizeof(int));//这个好像怪怪的,但没有又不行
        pt=(int*)malloc(n);
    }
    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);
    a.create();
    a.showNum();
    a.delSame();
    a.show();
    system("pause");
    return 0;
}
#11
joker392008-09-13 22:22
我倒,兄弟你都没运行一下吗?一运行就自动关闭了啊;我自己改了改
#12
joker392008-09-13 22:23
#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;
}
1