Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Initializing an array

Posted on 2000-03-24
5
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
gurpreets earned 400 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

715 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