use const std::string & instead of const char *. avoid exception in sfree()

This commit is contained in:
Axel Kohlmeyer
2021-08-06 08:38:06 -04:00
parent 912a1fc561
commit 8baaed5724
2 changed files with 13 additions and 19 deletions

View File

@ -141,9 +141,9 @@ namespace ReaxFF
// toolbox
extern void *scalloc(LAMMPS_NS::Error *, rc_bigint, rc_bigint, const char *);
extern void *smalloc(LAMMPS_NS::Error *, rc_bigint, const char *);
extern void sfree(LAMMPS_NS::Error *, void *, const char *);
extern void *scalloc(LAMMPS_NS::Error *, rc_bigint, rc_bigint, const std::string &);
extern void *smalloc(LAMMPS_NS::Error *, rc_bigint, const std::string &);
extern void sfree(LAMMPS_NS::Error *, void *, const std::string &);
// torsion angles

View File

@ -36,13 +36,12 @@
namespace ReaxFF {
/* safe malloc */
void *smalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, const char *name)
void *smalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, const std::string &name)
{
void *ptr;
if (n <= 0) {
auto errmsg = fmt::format("Trying to allocate {} bytes for array {}. "
"returning NULL.", n, name);
auto errmsg = fmt::format("Invalid size {} for array {}. Returning NULL.", n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr);
@ -51,8 +50,7 @@ namespace ReaxFF {
ptr = malloc(n);
if (ptr == nullptr) {
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}",
n, name);
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr);
}
@ -61,21 +59,19 @@ namespace ReaxFF {
}
/* safe calloc */
void *scalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, rc_bigint size, const char *name)
void *scalloc(LAMMPS_NS::Error *error_ptr, rc_bigint n, rc_bigint size, const std::string &name)
{
void *ptr;
if (n <= 0) {
auto errmsg = fmt::format("Trying to allocate {} elements for array {}. "
"returning NULL.\n", n, name);
auto errmsg = fmt::format("Invalid size {} for array {}. Returning NULL.\n", n, name);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr);
return nullptr;
}
if (size <= 0) {
auto errmsg = fmt::format("Elements size for array {} is {}. "
"returning NULL", name, size);
auto errmsg = fmt::format("Elements size for array {} is {}. Returning NULL", name, size);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr);
return nullptr;
@ -83,8 +79,7 @@ namespace ReaxFF {
ptr = calloc(n, size);
if (ptr == nullptr) {
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}",
n*size, name);
auto errmsg = fmt::format("Failed to allocate {} bytes for array {}", n*size, name);
if (error_ptr) error_ptr->one(FLERR,errmsg);
else fputs(errmsg.c_str(),stderr);
}
@ -93,12 +88,11 @@ namespace ReaxFF {
}
/* safe free */
void sfree(LAMMPS_NS::Error* error_ptr, void *ptr, const char *name)
void sfree(LAMMPS_NS::Error *error_ptr, void *ptr, const std::string &name)
{
if (ptr == nullptr) {
auto errmsg = fmt::format("Trying to free the already free()'d pointer {}",
name);
if (error_ptr) error_ptr->one(FLERR,errmsg);
auto errmsg = std::string("Trying to free the already free()'d pointer: ") + name;
if (error_ptr) error_ptr->one(FLERR, errmsg);
else fputs(errmsg.c_str(),stderr);
return;
}