Answer for array(u32 start_count)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
thenewkgb
Posts: 51
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Answer for array(u32 start_count)

Post by thenewkgb »

Greetings.
I would like to initialise members in my classes.
If I type

Code: Select all

irr::core::array<irr::scene::ISceneNode*> m_grid_scaffold(0);
then I'm told off by the compiler.
I used this page to see it's possible...
https://irrlicht.sourceforge.io/docu/cl ... array.html

If I look at the irrArray.h to see if start_count exists, I see only these members...

Code: Select all

private:
T* data;
u32 allocated;
u32 used;
TAlloc allocator;
eAllocStrategy strategy:4;
bool free_when_destroyed:1;
bool is_sorted:1;
Is it possible to set aside zero memory with (0), just to keep bugs at bay? I'm saying uninitialised variables are bad.
thenewkgb
Posts: 51
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: Answer for array(u32 start_count)

Post by thenewkgb »

Actually I saw something else too. Similar.
This is the SMaterial docs page...
https://irrlicht.sourceforge.io/docu/cl ... erial.html
I see I can create a blank, white material, that can be made with the constructor.
So if I try this

Code: Select all

irr::video::SMaterial my_mat();
driver->setMaterial(my_mat);
the compiler can't convert it. No Material() to Material.

The error with core::array was "expected a type specifier".
CuteAlien
Admin
Posts: 9638
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Answer for array(u32 start_count)

Post by CuteAlien »

The my_mat() case... don't use the brackets. Just my_mat; Otherwise it looks like a function call for this to c++. And yeah it's a bit confusing as it works with parameters but not with an empty constructor. Just a c++ thing, stackoverflow has more info about that: https://stackoverflow.com/questions/180 ... y-brackets

The irr::core::array<irr::scene::ISceneNode*> m_grid_scaffold(0); should actually compile (does here), but won't do what you expect.
The number says how many elements it should put on the array to start with. But it will indeed not initialize that memory. At least not for pointers. I think if you use objects it might call the default-constructor of those objects (I think std::vector actually allows that when you pass a second parameter so you can pass other constructors as well, a missing feature in Irrlicht so far!). So this is useful if you want to pre-allocate memory, when you know the size your array will need. Reason to do that sometimes is that it avoids adding to an array at the end which can triggering expensive memory re-allocations. But yeah, downside when doing that with pointers is - your array will have uninitialized values and you have to be careful.

Note that irr::core::array does initialize it's own member variables in the constructor if that's what you meant. Constructors in c++ can do that. Or if you code in a bit more modern c++ style (compared to what Irrlicht uses) you can just initialize member variables directly where you declare them and those initializations will be used in the constructor.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thenewkgb
Posts: 51
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: Answer for array(u32 start_count)

Post by thenewkgb »

Hey

What I most likely meant with constructors wasn't

Code: Select all

()
but I meant

Code: Select all

{}
.

Yes vector has ()'s for allocating space.
Post Reply