Solved

Initializing an array

Posted on 2000-03-24
5
200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Losing latter half of command line in Visual Studio C++ online program 10 84
Socket Programming (Unix) 8 142
How to convert MFC APP to Win32 APP. 19 94
max float value 3 51
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

828 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