[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1732965475924.png (1.72 MB, 3377x3795)
1.72 MB
1.72 MB PNG
Why doesn't std::vector use realloc?
Do Sepples programmers ever profile their code?
>>
The problem lies with reallocs interface.
When calling realloc on memory that cannot be expanded without moving the data, it allocates new memory and memcopies the data to the newly allocated buffer.
This is a problem, because if you want to move a variable in C++, you need to call the move constructor, since a variable might have a pointer to itself or one of its members (e.g. small vector optimization).
>>
>>106995153
You should never use realloc. The best solution is to use large enough chunks an linear growing, it will decrease the performance because of data locality but it can guarantee the elements in the array will be always in the same memory position.



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.