desktop: don't access first data element if no cylinders

TabDiveInformation::updateProfile() does some statistics via the
per_cylinder_mean_depth function. It passes down arrays with one
entry per cylinder, which are allocated by means std::vector.

To pass the array, the expression "&vector[0]" is used. It seems
like some compilers through an assertion violation if vector
has no elements. They are technically correct in that this is
undefined, but still this appears like very unfriendly behavior.
After all, std::vector should behave just like a dynamic C-array
that is automatically freed, when going out of scope.

Replace the "&vector[0]" by "vector.data()" and don't do the
call if there aren't any cylinders for good measure.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-04-21 08:59:33 +02:00 committed by Dirk Hohndel
parent 4aebcdc021
commit a8aa897117

View file

@ -128,7 +128,8 @@ void TabDiveInformation::updateProfile()
volume_t *gases = get_gas_used(current_dive);
QString volumes;
std::vector<int> mean(current_dive->cylinders.nr), duration(current_dive->cylinders.nr);
per_cylinder_mean_depth(current_dive, select_dc(current_dive), &mean[0], &duration[0]);
if (current_dive->cylinders.nr >= 0)
per_cylinder_mean_depth(current_dive, select_dc(current_dive), mean.data(), duration.data());
volume_t sac;
QString gaslist, SACs, separator;