summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGreg Ercolano <erco@seriss.com>2022-11-06 21:59:36 -0800
committerGreg Ercolano <erco@seriss.com>2022-11-07 10:33:35 -0800
commit2a43a12b7b870c6fd775f417c1c2e85222e47238 (patch)
treed4f8f82f0ac774d98f7aa1b8bcc2c3d134954833 /src
parent51ce2b9235f387e98c52374e4b7b5c906d7d778d (diff)
Added empty(), ensure size() enlarges new vals = 0
Diffstat (limited to 'src')
-rw-r--r--src/Fl_Int_Vector.cxx22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/Fl_Int_Vector.cxx b/src/Fl_Int_Vector.cxx
index 08e45c4cc..0815661dc 100644
--- a/src/Fl_Int_Vector.cxx
+++ b/src/Fl_Int_Vector.cxx
@@ -36,24 +36,30 @@ Fl_Int_Vector::~Fl_Int_Vector() {
/**
Set the size of the array to \p count.
- A size of zero empties the array completely and frees all memory.
+ Setting size to zero clears the array and frees any memory it used.
+
+ Shrinking truncates the array and frees memory of truncated elements.
+ Enlarging creates new elements that are zero in value.
\warning
- - Only advised use currently is to shrink the array size, i.e. (count < size()).
- - Currently enlarging the array leaves the new values uninitialized.
- - When assignment via indexes is supported, i.e. v[x] = 123, array enlargement should zero new values
- \todo Check if count > size, and if so init new values to 0.
+ Only currently advised use is to shrink the array size, i.e. (count < size()),
+ since assignment via index (e.g. v[x] = 123) is not yet supported.
*/
void Fl_Int_Vector::size(unsigned int count) {
- if (count <= 0) {
+ if (count == 0) { // zero? special case frees memory
if (arr_)
free(arr_);
arr_ = 0;
size_ = 0;
return;
}
- if (count > size_) {
+ if (count > size_) { // array enlarged? realloc + init new vals to 0
arr_ = (int *)realloc(arr_, count * sizeof(int));
- size_ = count;
+ while ( size_ < count ) {
+ arr_[size_++] = 0;
+ }
+ return; // leaves with size_ == count
}
+ // count <= size_? just truncate
+ size_ = count;
}