473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dereferencing a pointer to an array of pointers to objects

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
4 1488
> Is there a better way?
#include<vector>

....

Jul 23 '05 #2
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.