Dereferencing a pointer to an array of pointers to objects

R

Richard Cavell

Hi,

I want to create an arbitrary number of objects, whose size will vary.
I want to keep track of them. It seems best to create an array of
pointers to these objects. Since the array itself will have its size
determined at runtime (since I don't know how many objects), I want to
dynamically allocate the array and have a pointer to the array. The
objects in question are GMP large integers.

#include <gmp.h> // mpz_t is defined.

int i;
mpz_t** pp_bigints;
pp_bigints = new mpz_t*[NumberOfMPZNeeded];

for (i=0; i<=NumberOfMPZNeeded ;i++)
{
mpz_t* p_bigint = new mpz_t;

// This fails: cannot convert __mpz_struct* to __mpz_struct (*)[1]

}

for (i=0; i<=NumberOfMPZNeeded;i++)
delete pp_bigints;
delete[] pp_bigints;

Then I need to dereference pp_bigints, then fill array slot i with
p_bigint, and assign a created mpz_t to what it points to.

Is there a better way?
 
S

Saurabh Aggrawal

Since the array itself will have its size
determined at runtime
I want to
dynamically allocate the array and have a pointer to the array


I think you should use the link list instead as you don't know the size
beforehand.
#include <list>

Saurabh Aggrawal
 
R

Richard Cavell

#include<vector>

Okay, how about:

#include <gmp.h>

vector<mpz_t> MyBigInts;

MyBigInts.resize (NumberNeeded);
for (int i = 0; i < NumberNeeded; i++)
{
mpz_t MyMPZ;
MyBigInts.at(i) = MyMPZ;
}

vector<mpz_t>::iterator iter;
for (iter = MyBigInts.begin(); iter != MyBigInts.end(); iter++)
{
Now do something on *iter
}

Now:

1. I trust that MyBigInts will contain *copies* of the MyMPZ's that I
created. This means it will invoke the copy constructor?

2. When MyBigInts goes out of scope (end of program) all its contents
will be automatically destroyed?

3. I don't need any pointers?

4. I can operate on the individual elements like this and it provides
no overhead by copying the elements:

mpz_cmp (MyBigInts.at(0), MyOtherBigInt);
 
K

Karl Heinz Buchegger

Richard said:
Okay, how about:

#include <gmp.h>

vector<mpz_t> MyBigInts;

MyBigInts.resize (NumberNeeded);
for (int i = 0; i < NumberNeeded; i++)
{
mpz_t MyMPZ;
MyBigInts.at(i) = MyMPZ;
}

vector<mpz_t>::iterator iter;
for (iter = MyBigInts.begin(); iter != MyBigInts.end(); iter++)
{
Now do something on *iter
}

Now:

1. I trust that MyBigInts will contain *copies* of the MyMPZ's that I
created. This means it will invoke the copy constructor?

Yes
All standard containers work this way.
You create a vector of mpz_t. Thus the container manages everything
related to mpz_t

If you had a
vector< mpz_t* >
then the container would manage everything related to the pointers
themselfs, not the objects the pointer points to.
2. When MyBigInts goes out of scope (end of program) all its contents
will be automatically destroyed?
Yes


3. I don't need any pointers?
No


4. I can operate on the individual elements like this and it provides
no overhead by copying the elements:

mpz_cmp (MyBigInts.at(0), MyOtherBigInt);

Depends on the defintion of mpz_cmp.

If it is

bool mpz_cmp( const mpz_t& First, const mpz_t& Second );

then yes, no copies are made. (The important point is that the
function takes references. at() gives a reference, thus no copy
is necessary)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top