I constructed a irr::core::array with array(u32 start_count) to allocate some memory.
But array.size() returns 0.
[no bug] array.size() returns 0 after allocation
-
- Posts: 58
- Joined: Tue Apr 10, 2007 7:49 pm
- Location: Karlsruhe
-
- Posts: 58
- Joined: Tue Apr 10, 2007 7:49 pm
- Location: Karlsruhe
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.
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.
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.
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 58
- Joined: Tue Apr 10, 2007 7:49 pm
- Location: Karlsruhe
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.
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.
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.
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm