Initializing node from DataType does not set array length.
BradWhitlock opened this issue · comments
Node::init(DataType) checks whether the input DataType is compatible with the node's existing datatype. If the DataTypes are compatible then Node::init returns early. It does not copy the input DataType into the Node's dtype though. The goal of the early return is probably to skip a memory reallocation since the new data can fit in the storage that is already allocated. However, this means that the length of the node cannot be relied upon when the new DataType array length is shorter than the starting array length.
Would it be appropriate to set the node's dtype even if they are compatible so the array length will be correct when queried?
Or, should code be more liberal about using Node::reset() beforehand?
Node n;
n.set(std::vector<double>{0.,1.,2.,3.,4.,5.,6.,7.,8.,9.});
n = DataType(n.dtype().id(), 5); // This keeps the original 10 element array
auto a = n.as_double_array();
for(int i = 0; i < 5; i++)
a[i] = 10. + i;
// This will think a is 10 elements long.
EXPECT_EQ(a.number_of_elements(), 5);
// n still thinks it is 10 elements long.
n.print();