Pointers to pointers

J

Jonas Cord

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
 
R

Rolf Magnus

Jonas said:
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 = new int[100];

//use it

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

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

Ioannis Vranos

Rolf said:
Pretty much the same.

int main()
{
int** data;

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

//use it

for (int i = 0; i < 100; ++i)
delete [] data;
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];
 
V

velthuijsen

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>
}
}
 
J

Jonas Cord

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
 
I

Ioannis Vranos

Jonas said:
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[0]<<"\t"<<r[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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top