By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Dereferencing a pointer to an array of pointers to objects

P: n/a
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[i];
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?
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> Is there a better way?
#include<vector>

....

Jul 23 '05 #2

P: n/a
>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

Jul 23 '05 #3

P: n/a
On 16/2/05 2:23 PM, Phil Staite wrote:
Is there a better way?


#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);
Jul 23 '05 #4

P: n/a
Richard Cavell wrote:

On 16/2/05 2:23 PM, Phil Staite wrote:
Is there a better way?
#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?


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)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.