简体   繁体   中英

how to create array of objects without dynamic memory allocation

I got following code, where 100 objects are created in a loop.

My question is,

  • why do I need the use new here, because i already know beforehand how much memory i need.
  • is there a code which does the same as my example code, without dynamic memory allocation? (and without using std containers such as vector ) (...class + dynamic momery together is getting confusing)

example code:

class Particle {...};
Particle *myParticles[ 100 ];

for( int i = 0; i < 100; i++ )
{   
    myParticles[ i ] = new Particle(x,y);          //x and y are randomized for each loop
}

[UPDATE]: class Particle does not have a default constructor. Hence Particle myParticles[ 100 ]; will not work. Note that, there is a work around by setting up a default constructor, and then creating methods Particle::setXY (double x, double y) .

But is there a way to solve this without creating new methods?? ie only using constructor, and no dynamic memory allocation.

I just find it strange, that this cannot be done(?) without dynamic memory allocation. Or do I have to accept the fact that this just a quirk of C++ language??

You don't have to use new . You can create an array of Particle s as you suggested yourself:

Particle myParticles[100];

The thing is, it will be default-constructed as you cannot specify (x,y) parameters which are only available at runtime. So you will have to provide means to specify this information after the construction of a Particle , for example like this:

for( int i = 0; i < 100; i++ )
{   
    myParticles[i].SetCoords(x,y);          //x and y are randomized for each loop
}

This requires both default constructor for Particle and a method like SetCoords to configure the instance after construction. So it might not be an option if the code for Particle is beyond your control.

But I would really consider using vector as it's a very convenient and useful thing. It will be quite efficient as it uses very little number of dynamic allocations if programmed correctly. For example:

vector<Particle> myParticles;

myParticles.reserve(100);
for( int i = 0; i < 100; i++ )
{   
    myParticles.push_back(Particle(x,y));          //x and y are randomized for each loop
}

This should use just a single allocation. This assumes that Particle is fairly lightweight to be copied by a copy constructor.

why do I need the use new here, because i already know beforehand how much memory i need?

The reason is that probably this piece of code exists in a function. Creating an array local to the function and returning pointer/reference to it would result in Undefined behavior. So maybe the reason is to explicitly manage the lifetime of the created array elements . There is no way to tell exactly unless you show more code.

Is there a code which does the same as my example code, without dynamic memory allocation?

In general if given a choice,

Particle myParticle[100];

is much better and less error prone than using dynamic memory allocations.

Good Read:
Why should C++ programmers minimize use of 'new'?

Note that while it is good to limit the use of new as much as possible, the purpose of existence of new is because it is needed for some situations where it is more suited for the behavior desired. So it is more of an horses for courses.

Particle myParticles[ 100 ];

will use the default constructor 100 time and not Particle(x,y)

So, the real problem is that you have no way of passing argument to the constructor in the array.

If Particle has not default constructor Particle myParticles[ 100 ]; will not compile

We need more information. It could be that Particle is big, but we want to sort the array. Having an array of pointers you can swap it, and this could be a lot faster than swap Particles. The implementation of swap of Particles could be far from trivial. Finally you can pass the array calling other functions with make a copy of these pointers... Well complicate.

Being a C programmer that's how I'd roll:

char particle_space[sizeof(Particle) * 100];
Particle *myParticles = (Particle *) particle_space;

Then fill it the following way:

for (int i = 0; i < 100; i++)
    myParticles[i] = Particle(i, i);

I don't know if using sizeof in declaration is portable or if it's gcc's extension but that worked for me.

Particle *myParticles[ 100 ];

is array of Particle pointers, not Particles. And myParticles type is Particle** not just Particle*, that is why you need to allocate memory.

Particle myParticles[ 100 ];

would indeed create an array of Particle and there will be no need in mem allocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM