# Initializing arrays

## Recommended Posts

Harryu    174
I declare a pointer to an "sParticle" structure like this: sParticle *m_pParticles; In the sParticle structure, I have two constructors, 1 that takes multiple parameters, 1 that takes none. When I want to initialize m_pParticles, I do this: m_pParticles = new sParticle[512](Parameters...); Doing this results in a compiler error saying: error C2075: 'Target of operator new()' : array initialization needs curly braces However, when I initialize the pointer using the constructor that takes NO parameters, like this: m_pParticles = new sParticle[512](); It works perfectly. Is there a way I can make the first method work?

##### Share on other sites
Kizsam    133
hello

I am not really sure how to get it to work. I just thought that I would make a suggestion that may work. You may have already tried...

Try this:
m_pParticles = new (sParticle[512])(Parameters...);

can't hurt to try :).

regards.

##### Share on other sites
Harryu    174
Well, I hadn't tried it, but unfortunately it didn't work.

Thanks for the suggestion though.

##### Share on other sites
Guest Anonymous Poster
m_pParticles = new sParticle( parameters )[512];

##### Share on other sites
Harryu    174
Nope, that didn't work either.

##### Share on other sites
jfclavette    1058
Simply put, it's not possible. An alternative is:

sParticle **m_pParticles;m_pParticles = new sParticle*[512];for(int i=0; i<512; ++i) {  m_pParticles[i] = new sParticle(arg);}//Don't forget to delete after use.

or, if you can live without the constructor:

sParticle *m_pParticles;m_pParticles = new sParticle[512];for(int i=0; i<512; ++i) {  m_pParticles.setSomeStuff(foo);}

##### Share on other sites
Brother Bob    10344
If you don't want an array of pointers, but still want to use a constructor, I think placement new would work.
// allocate memorysParticle *m_pParticles = reinterpret_cast<sParticle *>(operator new(512*sizeof(sParticle)));// use placement new to construct the object in user-provided memoryfor(int i = 0; i < 512; ++i){    new(m_pParticles + i) sParticle(parameters ...);}

Although this requires you to manually trigger the destructors aswell.
// call destructorfor(int i = 0; i < 512; ++i){    m_pParticles[i].~sParticle();}// release memoryoperator delete(m_pParticles);

Tried it, and seems to work, but not 100% sure it's correct. If it isn't please say so [grin]

##### Share on other sites
snk_kid    1312
Quote:
 Original post by xMcBaiNx...Simply put, it's not possible...

Yes it is possible and there are two methods to do it:

1. non-recommended method without using standard library algorithms:

#include <new> // placement new//....size_t N = 512;// allocates onlysParticle* s = static_cast<sParticle*>(::operator new(sizeof(sParticle) * N));for(int i = 0; i < N; ++i)    ::new(s + i) sParticle(param); // constructs only, placement new operatorfor(int i = 0; i < N; ++i)    s[i].~sParticle(); // destroy only, must be done for NON POD-types with non trivial destructors::operator delete(s); // deallocate only

2. recommended method

#include <vector>//....std::vector<sParticle> v(size, param);

@Harryu: Prefer std::vector for dynamic arrays in C++ over C-style dynamic arrays period. Don't use method 1 99% of the time, std::vector already works similar to this but takes advantage of optimizations you've never heard of before.

##### Share on other sites
Brother Bob    10344
Quote:
 Original post by snk_kid2. recommended method#include //....std::vector v(size, param);

Indeed the recommended method, but shouldn't it be
std::vector<sParticle> v(size, sParticle(param));

##### Share on other sites
snk_kid    1312
Quote:
Original post by Brother Bob
Quote:
 Original post by snk_kid2. recommended method#include //....std::vector v(size, param);

Indeed the recommended method, but shouldn't it be
std::vector<sParticle> v(size, sParticle(param));

If the constructor has been declared explicit or has more than one parameter with no default values then yes other wise it's valid because it's implicitly converted, they are equivalent.