C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New function for std::vector

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sat, 28 Dec 2024 11:50:50 +0000
On Sat, 28 Dec 2024, 09:54 Фаррах Фаттахов via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> Sorry for the long wait, but I'm at the computer now and I wrote some code
> explaining how my dynamic array works.
> ```C++
> int main() {
> FE2D::dynamic_array<int, 5> arr; // Dynamic Array Initialized with
> start Capacity 5, but it will NOT intialize the elements
> // If you try to get they like arr[0] = 4;
> // you will get an error
>
> // Here is the function add() of my dynamic array
> /*
> // Add an Element to the Dynamic Array
> void add(T load) {
> mSize++;
> if (mSize > mCapacity) set_capacity(mSize);
> mData[this->last()] = load;
> }
> */
>
> // Yes, it's pretty simple, and i know that it won't work with for
> example std::unique_ptr,
>

And that's why it's completely unsuitable for inclusion in the standard.
This is broken code.

There's a reason std::vector works the way it does: because it's correct.



but now it's like this
>
>
> // NOTE :
> /*
> Initially FE2D::dynamic_array was not needed as a full replacement
> for std::vector,
> but rather as a maximally optimized dynamic array that almost does
> not worry about memory leaks.
>
> And in order to manage it, you needed to keep something in your
> head, and not rely on it, but then everything changed,
> I realized that this is very incomfortable and it would be easier
> if the array will be at least a little safer,
> and after that I decided to make it like std::vector, maximum
> safely,
> but possibly adding some specific functions, for example the same
> starting capacity
> */
>
> // Add 4 Elements to the Dynamic Array
> arr.add(1);
> arr.add(4);
> arr.add(6);
> arr.add(3);
>
> arr.show_info();
> // As i said : "Initially FE2D::dynamic_array was not needed as a full
> replacement for std::vector,
> // but rather as a maximally optimized dynamic array that almost does
> not worry about memory leaks."
> // So this function show_info() is old, and it don't care abot memory
> leaks.
> // I don't use it usually, but it may be comfortable if you working
> with simple values. For example here.
> // But i'm going to delete it soon, because it's really scary thing,
> it's using just std::cerr
>
> // Then use reset() function, which will set mSize to zero, but will
> NOT touch the capacity
> arr.reset();
>
> // Then we can add new elements to the old places, cuz mSize is 0
> arr.add(3);
> arr.add(6);
> arr.add(4);
> arr.add(1);
>
> arr.show_info();
>
> return 0;
> }
> ```
>
> OUTPUT :
> Type : int
> Size : 4
> Capacity : 5
>
> 0 : 1
> 1 : 4
> 2 : 6
> 3 : 3
>
>
> Type : int
> Size : 4
> Capacity : 5
>
> 0 : 3
> 1 : 6
> 2 : 4
> 3 : 1
>
> сб, 28 дек. 2024 г. в 12:18, Bo Persson via Std-Proposals <
> std-proposals_at_[hidden]>:
>
>> On 2024-12-28 at 01:05, Andre Kostur via Std-Proposals wrote:
>> > Resize erases the number of “excess” elements, but where does it say
>> > that it affects the capacity?
>> > [vector.capacity]. Erase doesn’t seem to talk about reducing the
>> > capacity either [vector.modifiers].
>>
>> The spec is under reserve(), which claims that no reallocations will
>> happen until the size surpasses the new capacity. This would not work if
>> other members could reduce the capacity.
>>
>>
>>
>> >
>> > On Fri, Dec 27, 2024 at 2:51 PM Chris Ryan <chrisr98008_at_[hidden]
>> > <mailto:chrisr98008_at_[hidden]>> wrote:
>> >
>> > Hi Andre,
>> > .resize(0) actually deletes the storage. Farrakh is wanting it to
>> > preserve the storage (keep the .capacity(), not have to alloc it
>> again)
>> >
>> > Farrakh,
>> > I think that you might not realize the reason .clear() is slower is
>> > because it has to call the in-place destructor for each element
>> > (zero through size(), but not all the way up to .capacity()) You
>> > can't just change the count or you screw up the object lifetime of
>> > those objects that were not destructed. D'tors won't get called.
>> > Index positions may potentially get overwritten later with
>> > subsequent push_backs(...) and the in-place c'tors calls. (thus
>> > causing leaks)
>> >
>> > https://en.cppreference.com/w/cpp/container/vector/clear <https://
>> > en.cppreference.com/w/cpp/container/vector/clear>
>> >
>> >
>> > On Fri, Dec 27, 2024 at 2:08 PM Andre Kostur via Std-Proposals <std-
>> > proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
>> > wrote:
>> >
>> > What’s wrong with .resize(0) ?
>> >
>> > On Fri, Dec 27, 2024 at 1:57 PM Фаррах Фаттахов via Std-
>> > Proposals <std-proposals_at_[hidden] <mailto:std-
>> > proposals_at_[hidden]>> wrote:
>> >
>> > After working with SFML, I so hated std::vector (I was a bad
>> > programmer back then and didn't know about the reserve()
>> > function, which led to not understanding why std::vector is
>> > so slow, while a simple array performs much better). As a
>> > result, in my new project i created my own version of
>> > std::vector - FE2D::dynamic_array. And there, from the
>> > beginning i created one useful function - reset()
>> > ```C++
>> >
>> > // Reset the Size of Dynamic Array to Zero, but it's don't
>> > Touches the Real Occupied Memory of Dynamic Array void
>> > reset() { mSize = 0; }
>> > ```
>> >
>> > Instead of clear() it's just setting mSize to zero. It's
>> > very useful in the situations like
>> >
>> > ```C++
>> > void Render() { // It's called every frame // Add a sprites
>> > to the FE2D::dynamic_array to draw it later
>> > m_Renderer.AddSprite(m_Sprite_0);
>> > m_Renderer.AddSprite(m_Sprite_1);
>> > m_Renderer.AddSprite(m_Sprite_2); ... // Draw the Sprites
>> > and reset the dynamic arrays to prepare it for the next
>> > frame m_Renderer.RenderSprites(); }
>> > ```
>> > Here reset() works like clear(), but much faster
>> >
>> > In the std::vector or std::deque is no functions like
>> > reset() and i want to add it there, especially sometimes i
>> > get errors because my FE2D::dynamic_array is not so good
>> > like std::vector and i have to use it instead of mine, but
>> > there is no function i need. I think reset() will be nice
>> > thing for std::vector.
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-12-28 11:52:09