This topic is now archived and is closed to further replies.

Jesper T

Arrays of objects - how to make ?

Recommended Posts

Ok right, I have made a class called "Delta", and the way I have used it up till now is like this: Delta *dobj = new Delta(); Now i want to create an array of delta class objects (or rather an array of pointers to delta objects) I tried various versions like this: Delta *dobj[10] = new Delta(); and Delta *dobj = new Delta()[10]; etc... .. and got errors when compiling. So.. I''d appreciate it if anyone would like to give me a hint JT

Share this post

Link to post
Share on other sites
class Delta {
int x;
Delta() {x=0;};
Delta(int x1) {x=x1;};

imagine this was your class.

Delta array[10];
would make an array... of 10 Deltas... using the Delta() constructor function.

ie array[0].x==0;array[1].x==0; etc...

you can just declare a delta as

Delta element; // the () is implied, as it is the default constructor

Delta *dobj=new Delta(); // This is just overly complex... if you need a pointer to an object of your class... use the dereference operation... ie *(&element)=element ie &element, is a pointer to element.

Share this post

Link to post
Share on other sites
Well ok, thanks for the help, though i found another way:

Delta *dobj[10] = { new Delta(), new Delta(), ... };

The reason why i used pointers is that I think I read somwhere that it was more memory economical (something combined with the ~Delta() err.. called default destructor ? i may be wrong )

Share this post

Link to post
Share on other sites
economical? Well the only thing that it does is dynamicaly create objects in memory on the fly.

and yes ~Delta is the default destructor. If you use the class wizard it creates this for you

"There is humor in everything depending on which prespective you look from."

Share this post

Link to post
Share on other sites
not really.... if you need to have 10 objects of Class delta... you need to allocate memory to store them.... you see, an array, like

Delta element[10];

the object "element" is simply a pointer, and by typing element[x] you just use offset addressing to get the x(th) element. So to do this, you need memory for 10 delta elements, and 1 pointer to Delta...

Pointers are very useful for having as arguments for functions... Imagine I wanted to process an array within a function... if I sent all the array elements... the compiler would give me a local copy of all teh elements... then I would edit them.. and put them back on the originals... so you need to move all teh data twice.... If my function uses a pointer to the first element in the array... all I need to copy is the pointer, to a local variable... and work with what it points to... That is the advantage of pointers (or one of them). With big classes and data structures, pointers can provide a huge speed increase, by decreasing the amount of copying necessary

Share this post

Link to post
Share on other sites
OK, when you have arrays of objects you generally want to have
pointers to objects, because then you explicity construct each object, otherwise the compiler will construct the objects for you. I seem to rembmer reading somewhere that arrays of objects are simply initialised to zeros, the constructor isn''t called for each object.

To have an array of pointers to objects, define
Delta *dobjArray[10]; //makes an array of pointers

then for each element in the array
dobjArray[x] = new Delta();

to have a dynamic array of Deltas

Delta **obj;

*obj = new Delta *[size];

obj[x] = new Delta();

Actually I think I might be wrong just above, you might
have to use malloc for the first allocation of pointers.


Share this post

Link to post
Share on other sites
If you know the size of the array you want at compile time--10 in this example--you should be using this syntax:
Delta dobj[10];

This will allocate the memory for 10 delta objects, and call the default constructor for each of them. When dobj is destroyed, each Delta destructor will be called for each object.

If you do not know the size of the array at compile time--i.e. it''s a variable "num"--do it dynamically:
Delta *dobj = new Delta [num];

This allocates space for num Deltas and calls the default constructor on each of them. You must manually delete this array with:
delete [] dobj;
when you''re done, which will call the destructor on each of the delta objects and then free the associated memory.

The differences between doing it this way and doing it using pointers are:
- using a static or dynamic array like above causes the space occupied by dobj to be contiguous in memory. If the number of objects or the size of each object is large (such that the total is > 1 meg) this might be a problem.
- using pointers to each object like the poster suggests causes an extra level of indirection, causing a performance penalty, and allocates one pointer for each object, causing a memory penalty. However, the objects can be anywhere in memory they''d like to be (which can cause memory fragmentation, also possibly a bad thing).

Share this post

Link to post
Share on other sites