Solved

Initializing an array

Posted on 2000-03-24
5
198 Views
Last Modified: 2010-04-10
I have a class that hasn't got a default constructor and I want to make an array using new and initialize all its objects with the same value(s).What is the most elegent way to do this?
The current way I use is using a default constructor then calling an Init function using a loop, anything better?
0
Comment
Question by:karouri
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
gurpreets earned 100 total points
ID: 2656359
I will give the answer with the help of an example.

Consider a class for company equipment in which the corporate ID number of the equipment is a mandatory constructor argument.

class EquipmentPiece {
public:
  EquipmentPiece(int IDNumber);
  ...
};

It is not usually possible to create arrays of EquipmentPiece objects.

EquipmentPiece bestPieces[10];           // error! No way to call
                                         // EquipmentPiece ctors
 
 EquipmentPiece *bestPieces =
  new EquipmentPiece[10];                // error! same problem


There are three ways to get around this restriction. A solution for non-heap arrays is to provide the necessary arguments at the point where the array is defined.

int ID1, ID2, ID3, ..., ID10;            // variables to hold
                                         // equipment ID numbers
....

EquipmentPiece bestPieces[] = {          // fine, ctor arguments
  EquipmentPiece(ID1),                   // are provided
  EquipmentPiece(ID2),
  EquipmentPiece(ID3),
  ...,
  EquipmentPiece(ID10)
};

Unfortunately, there is no way to extend this strategy to heap arrays.

A more general approach is to use an array of pointers instead of an array of objects:

typedef EquipmentPiece* PEP;           //PEP is a pointer to an EquipmentPiece

PEP bestPieces[10]                    // fine, no ctors called

PEP *bestPieces = new PEP[10]           // also fine

Each pointer in the array can then be made to point to a different EquipmentPiece object:

for (int i = 0; i < 10; ++i)
  bestPieces[i] = new EquipmentPiece( ID Number );
There are two disadvantages to this approach. First, you have to remember to delete all the objects pointed to by the array. If you forget, you have a resource leak. Second, the total amount of memory you need increases, because you need the space for the pointers as well as the space for the EquipmentPiece objects.

You can avoid the space penalty if you allocate the raw memory for the array, then use "placement new" to construct the EquipmentPiece objects in the memory:
0
 
LVL 22

Expert Comment

by:nietod
ID: 2656421
There is another option.  use a containter class, like vector<>.  Then you can add items to the vector one-at-a-time specifying the values for the constructor each time.   Basically it is like using an array of pointers, but safer because you can't get memory leaks.
0
 
LVL 3

Author Comment

by:karouri
ID: 2657930
Thanks gurpreets,your answer is good,but what is:
>You can avoid the space penalty if you allocate the raw memory for the array, then use "placement new" to construct the EquipmentPiece objects in the memory

0
 
LVL 2

Expert Comment

by:gurpreets
ID: 2660261
What I meant, karouri, was that if u use placement new op to allocate a chunk a memory u don't need to free individual array elements. U can delete the whole space in one go. This way there is lesser probability of not freeing memory.
0
 
LVL 3

Author Comment

by:karouri
ID: 2677769
Thanks gurpreets,and sorry for being late but your answer is excellent..
Thanks too nietod.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now