f



Pointers to pointers

How can you use a "pointer to a pointer" as a two dimensional array
using 'new' and 'delete' rather than 'malloc' and 'free'?
Thanks,

Jonas

0
cordjonas (6)
4/13/2005 11:24:21 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

5 Replies
732 Views

Similar Articles

[PageSpeed] 56

Jonas Cord wrote:

> How can you use a "pointer to a pointer" as a two dimensional array
> using 'new' and 'delete' rather than 'malloc' and 'free'?

Pretty much the same.

int main()
{
    int** data;

    data = new int*[100];
    for (int i = 0; i < 100; ++i)
        data[i] = new int[100];

    //use it

    for (int i = 0; i < 100; ++i)
        delete [] data[i];
    delete [] data;
}

But you should consider using std::vector instead of plain arrays.

0
ramagnus (3487)
4/13/2005 12:19:12 PM
Rolf Magnus wrote:

> Pretty much the same.
> 
> int main()
> {
>     int** data;
> 
>     data = new int*[100];
>     for (int i = 0; i < 100; ++i)
>         data[i] = new int[100];
> 
>     //use it
> 
>     for (int i = 0; i < 100; ++i)
>         delete [] data[i];
>     delete [] data;
> }
> 
> But you should consider using std::vector instead of plain arrays.


In addition, I think it is useful to be mentioned here that one can define a 2-dimensional 
array in the free store like this:


int(*p)[3]= new int[2][3];


or

typedef int whatever[3];

whatever *p= new int[2][3];



or

typedef int whatever[3];

whatever *p =new whatever[2];



-- 
Ioannis Vranos

http://www23.brinkster.com/noicys
0
ivr208 (995)
4/13/2005 12:26:37 PM
First unless you are working with timecritical or space constrained
code I'd say use the STL container classes (Depending on what you need
select one).

Second I'd suggest using a single pointer if possible then do the
adressing of location inside the array yourself. That way you save
yourself some headaches at the cost of having to do the internals
yourself. Biggest thing you need to take into account is are you going
to do everything row first or column first.

something along the lines of:

const unsigned NrRows = 16;
const unsigned NrColumns = 10;

int *MyArr = new int[NrRows * NrColumns];

// row first so outerloop is columns, innerloop is rows
for (unsigned i =0; i < NrColumns;++i)
{
    for (unsigned j= 0; j < NrRows;++i)
    {
        MyArr[NrRows * i + j] = <value>
    }
}

0
velthuijsen (115)
4/13/2005 12:32:02 PM
Thanks to all who replied. I am trying to understand how multiple
subscripted arrays can be converted to multiple pointers.
For example, suppose I have an array

int q[3][2] = { 1,0,2,4,{3,6}};

i.e.

1 0
2 4
3 6

Now I convert it into an int[2] by

int (*r)[2] = q;

What does this mean exactly? I can see that *r[0] is 1, *r[1] is 2 and
*r[2] is 3. Yet according to a textbook I'm using *r[0] should be 1,
*r[1] should be 0 and *r[2] should be out of bounds...
If *r has indeed three elements, then why is it called an int[2]?
Thanks,

Jonas

0
cordjonas (6)
4/13/2005 12:48:31 PM
Jonas Cord wrote:
> Thanks to all who replied. I am trying to understand how multiple
> subscripted arrays can be converted to multiple pointers.
> For example, suppose I have an array
> 
> int q[3][2] = { 1,0,2,4,{3,6}};
> 
> i.e.
> 
> 1 0
> 2 4
> 3 6
> 
> Now I convert it into an int[2] by
> 
> int (*r)[2] = q;


This is a pointer to an array of 2 integers.


> What does this mean exactly? I can see that *r[0] is 1, *r[1] is 2 and
> *r[2] is 3. Yet according to a textbook I'm using *r[0] should be 1,
> *r[1] should be 0 and *r[2] should be out of bounds...
> If *r has indeed three elements, then why is it called an int[2]?
 >
> Thanks,
> 
> Jonas


q can be said to be an array of two integer-arrays.

r is a pointer to a 2-integer array;

so r[0] points to the first array of two integers, r[1] to the second array of two 
integers and so on.

*r[2] with r pointing to q, is equivalent to r[2][0], which is 3.


An example for printing the contents of q using a pointer to 2-integer array

[ (*r)[0] is different from *r[0] ]:


#include <iostream>

int main()
{
     using std::cout;

     int q[3][2] = { 1,0,2,4,{3,6}};

     int (*r)[2];


     for(r= q; r-q<3; ++r)
        cout<<(*r)[0]<<"\t"<<(*r)[1]<<"\n";


     cout<<"\n\n";

     r= q;

     for(unsigned i=0; i<3; ++i)
        cout<<r[i][0]<<"\t"<<r[i][1]<<"\n";
}



And my other free store code more complete:


int main()
{
     int(*p1)[3]= new int[2][3];

     delete[] p1;


     typedef int whatever[3];

     whatever *p2= new int[2][3];

     delete[] p2;


     typedef int what[3];

     what *p3 =new what[2];

     delete[] p3;
}



-- 
Ioannis Vranos

http://www23.brinkster.com/noicys
0
ivr208 (995)
4/13/2005 1:18:05 PM
Reply: