Bug #758

OFVector needs to seperate allocation and element construction

Added by Jan Schlamelcher 4 months ago. Updated 4 months ago.

Status:New Start date:2017-06-09
Priority:High Due date:
Assignee:- % Done:


Target version:3.6.3
Module: Compiler:
Operating System:


The current OFVector implementation does not separate element construction from memory allocation, i.e.

capacity() == 10
means at least 10 elements have been constructed even if
size() == 0
. This is not only agains the C++ standard but also has serious ramifications:
1. The element type used within an OFVector needs to be default constructible, which is an unnecessary constraint that leads to inefficient code.
2. Recursive structures based on OFVector cause a stack overflow!! See this example:
1struct Recursive : OFVector<Recursive>
3// data members
6Recursive r; // stack overflow, since the default constructor constructs elements, which themselves recursively construct more elements.


#1 Updated by Jan Schlamelcher 4 months ago

A partial workaround has been commited as #48e6422858b2a62 . This fixes issue 2. from above by removing the predictive allocation of some elements in OFVectors default constructor, which was probably a bad idea anyway, see for example: https://stackoverflow.com/questions/12271017/initial-capacity-of-vector-in-c . The commit also adds checks to the configuration test for std::vector and the unit test OFVector to ensure the problem will not be introduced again.

Also available in: Atom PDF