[no bug] array.size() returns 0 after allocation

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
MickeyKnox
Posts: 58
Joined: Tue Apr 10, 2007 7:49 pm
Location: Karlsruhe

[no bug] array.size() returns 0 after allocation

Post by MickeyKnox »

I constructed a irr::core::array with array(u32 start_count) to allocate some memory.
But array.size() returns 0.
loki1985
Posts: 214
Joined: Thu Mar 31, 2005 2:36 pm

Post by loki1985 »

AFAIK you can generate an array for a specific size (so it reserves space for said size), but the cells are still empty, and so of course it says size 0.

you need to push_back() elements first.
MickeyKnox
Posts: 58
Joined: Tue Apr 10, 2007 7:49 pm
Location: Karlsruhe

Post by MickeyKnox »

hm.. in my opinion the semantics of array.size() is to return the amount of merory that
has been allocated for the specified type, not the number of elements that have been
pushed into the array.

This is the semantic for std::vector.size(), and think this handled similar in Java.

Anyway, i think this should be clarified in the documentation.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

In irrArray size() is to return the size of array and allocated_size () is to return the amount of reserved memory. So far that is comparable to std::vector where size() also returns elements and capacity() returns the reserved memory.

But std::vector does indeed also set the size of the array on constructor so size() wouldn't be 0 anymore. So it does not just reserve memory but also initialize elements. Which is why you also can actually set an element as default parameter in std::vector, while irrlicht has nothing comparable. Then again just reserving memory in std without changing size needs 2 steps and you are not guaranteed afterwards that you got the exact size but only that you got at least that size.
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
MickeyKnox
Posts: 58
Joined: Tue Apr 10, 2007 7:49 pm
Location: Karlsruhe

Post by MickeyKnox »

hm.. first i decided to just accept your explanations. But then i tested another thing:

I have this array with some memory allocated, but not initialized. Now i initialize the elements
by hand in a for loop with the direct access operator [].

After that, the size is still 0.

In my opinion, an array is to be used primarily with the direct access operator (because
thats its big advantage over other containers), and the size method should still work.

Maybe this conflicts with other design choices, but i think this behavior bends the semantics
of an array quite a lot.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Maybe set_used will do what you need. That reserves items.

That operator[] is unsafe is ok imho as it just should be fast (you will get an assert when compiling the engine with _DEBUG). What the array is still missing is a safe alternative like std::vector::at(), but then again for some reason people I know never seem to did use that one.
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
Post Reply