Double Pointer.

Discussion in 'C++' started by Muzammil, Aug 19, 2008.

  1. Muzammil

    Muzammil Guest

    Hi.Can any one help me using double pointer with template on any
    class.
    i m confuse in using double pointer.its my class .
    //dynamic 2-d array class
    template <class t>
    class dsarray
    {
    private :
    unsigned int size;
    t** data;
    public:

    dsarray():data(NULL),size(0){}

    dsarray(int s):data(new t*),size(s)
    {
    for(int i =0;i<size;i++)data=new t[size];
    // memset(this->data,0,this->size*sizeof(t));
    }
    void getdata()
    {
    for (int i = 0;i < this->size*size;i++)
    {
    cout<<"Element # "<<i;
    cin>>this->*data;
    }
    }
    }
    int main()
    {
    dsarray<int> d1(2),d3(3);
    d1.getdata();
    system("pause");
    return 0;
    }

    here getdata(); have the problem in reading the input.
    Muzammil, Aug 19, 2008
    #1
    1. Advertising

  2. Muzammil

    Ian Collins Guest

    Muzammil wrote:
    > Hi.Can any one help me using double pointer with template on any
    > class.
    > i m confuse in using double pointer.its my class .
    > //dynamic 2-d array class


    Why don't you just use a vector or vectors?

    --
    Ian Collins.
    Ian Collins, Aug 19, 2008
    #2
    1. Advertising

  3. Muzammil

    Muzammil Guest

    Re: Double Pointer.

    i have to implement this .beacuse we are learning about double
    pointer(new in data structure course.in OOP we didn't pratise of it
    more.so now we have to work out.).

    Ian Collins wrote:
    > Muzammil wrote:
    > > Hi.Can any one help me using double pointer with template on any
    > > class.
    > > i m confuse in using double pointer.its my class .
    > > //dynamic 2-d array class

    >
    > Why don't you just use a vector or vectors?
    >
    > --
    > Ian Collins.
    Muzammil, Aug 19, 2008
    #3
  4. Muzammil

    Uwe Schmitt Guest

    Re: Double Pointer.

    On 19 Aug., 06:02, Muzammil <> wrote:
    > Hi.Can any one help me using double  pointer with template on any
    > class.
    > i m confuse in using  double pointer.its my class .
    > //dynamic 2-d array class
    > template <class t>
    > class dsarray
    > {
    >       private :
    >             unsigned int  size;
    >              t** data;
    >        public:
    >
    >        dsarray():data(NULL),size(0){}
    >
    >        dsarray(int s):data(new t*),size(s)


    here you allocate memory,

    >        {
    >        for(int i =0;i<size;i++)data=new t[size];


    and here again. this will result in memory leaks.
    you will need a destrutor too. but i do not think that
    this

    >     //   memset(this->data,0,this->size*sizeof(t));
    >        }
    >  void getdata()
    >        {
    >             for (int i = 0;i  < this->size*size;i++)
    >             {
    >                 cout<<"Element # "<<i;
    >                 cin>>this->*data;
    >             }
    >         }}
    >
    > int main()
    > {
    >     dsarray<int> d1(2),d3(3);
    >     d1.getdata();
    >    system("pause");
    >     return 0;
    >
    > }
    >
    > here  getdata(); have the problem in reading the  input.


    what do you mean ? what problem do you have ?

    Greetings, Uwe
    Uwe Schmitt, Aug 19, 2008
    #4
  5. Muzammil

    Uwe Schmitt Guest

    Re: Double Pointer.

    On 19 Aug., 08:33, Uwe Schmitt <> wrote:
    > On 19 Aug., 06:02, Muzammil <> wrote:
    >
    > > Hi.Can any one help me using double  pointer with template on any
    > > class.
    > > i m confuse in using  double pointer.its my class .
    > > //dynamic 2-d array class
    > > template <class t>
    > > class dsarray
    > > {
    > >       private :
    > >             unsigned int  size;
    > >              t** data;
    > >        public:

    >
    > >        dsarray():data(NULL),size(0){}

    >
    > >        dsarray(int s):data(new t*),size(s)

    >
    > here you allocate memory,
    >
    > >        {
    > >        for(int i =0;i<size;i++)data=new t[size];

    >
    > and here again. this will result in memory leaks.


    ups. i was to fast. no memory leak.

    > you will need a destrutor too. but i do not think that
    > this
    >
    >
    >
    > >     //   memset(this->data,0,this->size*sizeof(t));
    > >        }
    > >  void getdata()
    > >        {
    > >             for (int i = 0;i  < this->size*size;i++)
    > >             {
    > >                 cout<<"Element # "<<i;
    > >                 cin>>this->*data;
    > >             }
    > >         }}

    >
    > > int main()
    > > {
    > >     dsarray<int> d1(2),d3(3);
    > >     d1.getdata();
    > >    system("pause");
    > >     return 0;

    >
    > > }

    >
    > > here  getdata(); have the problem in reading the  input.

    >
    > what do you mean ? what problem do you have ?
    >
    > Greetings, Uwe
    Uwe Schmitt, Aug 19, 2008
    #5
  6. Muzammil

    Muzammil Guest

    Re: Double Pointer.

    thanks. i know i haven't make a destructor.

    Paavo Helde wrote:
    > Muzammil <> kirjutas:
    >
    > > Hi.Can any one help me using double pointer with template on any
    > > class.
    > > i m confuse in using double pointer.its my class .
    > > //dynamic 2-d array class
    > > template <class t>
    > > class dsarray
    > > {
    > > private :
    > > unsigned int size;
    > > t** data;
    > > public:
    > >
    > > dsarray():data(NULL),size(0){}
    > >
    > > dsarray(int s):data(new t*),size(s)

    >
    > Here, data is allocated as an array of size t* pointers.
    >
    > > {
    > > for(int i =0;i<size;i++)data=new t[size];

    >
    >
    > > // memset(this->data,0,this->size*sizeof(t));
    > > }
    > > void getdata()
    > > {
    > > for (int i = 0;i < this->size*size;i++)

    >
    > Here, i goes from 0 to size*size.
    >
    > > {
    > > cout<<"Element # "<<i;
    > > cin>>this->*data;

    >
    > "this->" is superfluous.
    >
    > Here, data has been allocated only size elements, so it will overflow
    > soon when i grows larger than size. Also, each data points to an array
    > of t-s, but you only attempt to store something in its first element only
    > (the * operator dereferences the array pointer, which gives you its first
    > element only).
    >
    > > }
    > > }
    > > }

    >
    > Missing semicolon. Also, your class does not have a proper destructor and
    > will leak memory.
    >
    > > int main()
    > > {
    > > dsarray<int> d1(2),d3(3);
    > > d1.getdata();
    > > system("pause");
    > > return 0;
    > > }
    > >
    > > here getdata(); have the problem in reading the input.
    > >
    Muzammil, Aug 19, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,484
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    10
    Views:
    695
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  4. asker123
    Replies:
    0
    Views:
    1,292
    asker123
    Jun 27, 2009
  5. Replies:
    4
    Views:
    1,249
    Fred Zwarts
    Jul 2, 2009
Loading...

Share This Page