Sign in to follow this  
Harryu

Initializing arrays

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
If you don't want an array of pointers, but still want to use a constructor, I think placement new would work.

// allocate memory
sParticle *m_pParticles = reinterpret_cast<sParticle *>(operator new(512*sizeof(sParticle)));

// use placement new to construct the object in user-provided memory
for(int i = 0; i < 512; ++i)
{
new(m_pParticles + i) sParticle(parameters ...);
}

Although this requires you to manually trigger the destructors aswell.

// call destructor
for(int i = 0; i < 512; ++i)
{
m_pParticles[i].~sParticle();
}

// release memory
operator 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 this post


Link to post
Share on other sites
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 only
sParticle* 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 operator

for(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 this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
Quote:
Original post by snk_kid
2. recommended method


#include <vector>

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


Indeed the recommended method, but shouldn't it be

std::vector<sParticle> v(size, sParticle(param));

instead?


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this