From 9732a1c5b7ca3291c1776a2b73446697ee5fe347 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 18 Mar 2021 15:59:34 -0600 Subject: [PATCH 1/2] cannot qsort size 0 vector --- basicsubs.cpp | 6 +++--- parse.cpp | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/basicsubs.cpp b/basicsubs.cpp index 26321559..208c14bb 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -2636,7 +2636,7 @@ edl_species(const char *surf_name, LDBLE * count, char ***names, LDBLE ** moles, /* * Sort system species */ - if (sys.size() > 1) + if (sys.size() > 0) { qsort(&sys[0], sys.size(), sizeof(struct system_species), system_species_compare); @@ -2727,12 +2727,12 @@ system_total(const char *total_name, LDBLE * count, char ***names, /* * Sort system species */ - if (sys.size() > 1 && isort == 0) + if (sys.size() > 0 && isort == 0) { qsort(&sys[0], sys.size(), sizeof(struct system_species), system_species_compare); } - else + else if (sys.size() > 0) { qsort(&sys[0], sys.size(), (size_t)sizeof(struct system_species), system_species_compare_name); diff --git a/parse.cpp b/parse.cpp index d5c59eed..147edd65 100644 --- a/parse.cpp +++ b/parse.cpp @@ -226,8 +226,6 @@ check_eqn(int association) /* * Sort elements in reaction and combine */ - //qsort(elt_list[0], (size_t) count_elts, sizeof(struct elt_list), - // elt_list_compare); if (elt_list_combine() == ERROR) return (ERROR); /* From 1ab86414f0b25f986c14a55bdb967dfdef3af2ae Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 18 Mar 2021 16:40:11 -0600 Subject: [PATCH 2/2] remove _v, use std::vector only, alloc at least 1 scratch --- Phreeqc.cpp | 7 ------- Phreeqc.h | 4 +--- cl1.cpp | 37 ++++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 440f3284..86eeba34 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -966,13 +966,6 @@ void Phreeqc::init(void) basic_callback_cookie = NULL; basic_fortran_callback_ptr = NULL; - /* cl1.cpp ------------------------------- */ - x_arg = NULL; - res_arg = NULL; - scratch = NULL; - x_arg_max = 0; - res_arg_max = 0; - scratch_max = 0; #ifdef SKIP /* dw.cpp ------------------------------- */ /* COMMON /QQQQ/ */ diff --git a/Phreeqc.h b/Phreeqc.h index 909fe234..b3a3598e 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1737,9 +1737,7 @@ protected: #endif /* cl1.cpp ------------------------------- */ - LDBLE *x_arg, *res_arg, *scratch; - std::vector x_arg_v, res_arg_v, scratch_v; - int x_arg_max, res_arg_max, scratch_max; + std::vector x_arg, res_arg, scratch; /* gases.cpp ------------------------------- */ LDBLE a_aa_sum, b2, b_sum, R_TK; diff --git a/cl1.cpp b/cl1.cpp index 868cdba0..fefba87d 100644 --- a/cl1.cpp +++ b/cl1.cpp @@ -849,24 +849,31 @@ cl1(int k, int l, int m, int n, void Phreeqc:: cl1_space(int check, int l_n2d, int klm, int l_nklmd) { - if (l_n2d > x_arg_v.size()) + if (check == 1) { - x_arg_v.resize(l_n2d); - x_arg = &x_arg_v[0]; - } - memset(&x_arg_v[0], 0, sizeof(double) * (size_t)l_n2d); + if ((size_t)l_n2d > x_arg.size()) + { + x_arg.resize((size_t)l_n2d); + } + memset(&x_arg[0], 0, sizeof(double) * (size_t)l_n2d); - if (klm > res_arg_v.size()) - { - res_arg_v.resize(klm); - res_arg = &res_arg_v[0]; + if ((size_t)klm > res_arg.size()) + { + res_arg.resize((size_t)klm); + } + memset(&res_arg[0], 0, sizeof(double) * (size_t)klm); } - memset(&res_arg_v[0], 0, sizeof(double) * (size_t)klm); - - if (l_nklmd > scratch_v.size()) + if (l_nklmd > 0) { - scratch_v.resize(l_nklmd); - scratch = &scratch_v[0]; + if ((size_t)l_nklmd > scratch.size()) + { + scratch.resize(l_nklmd); + } + memset(&scratch[0], 0, sizeof(double) * (size_t)l_nklmd); + } + else if (scratch.size() == 0) + { + scratch.resize(1); + memset(&scratch[0], 0, sizeof(double)); } - memset(&scratch_v[0], 0, sizeof(double) * (size_t)l_nklmd); }