diff --git a/src/KIM/kim_init.cpp b/src/KIM/kim_init.cpp index 36df368016..2d639ede4c 100644 --- a/src/KIM/kim_init.cpp +++ b/src/KIM/kim_init.cpp @@ -86,10 +86,8 @@ void KimInit::command(int narg, char **arg) if (domain->box_exist) error->all(FLERR,"Must use 'kim_init' command before " "simulation box is defined"); - char *model_name = new char[strlen(arg[0])+1]; - strcpy(model_name,arg[0]); - char *user_units = new char[strlen(arg[1])+1]; - strcpy(user_units,arg[1]); + char *model_name = utils::strdup(arg[0]); + char *user_units = utils::strdup(arg[1]); if (narg == 3) { if (strcmp(arg[2],"unit_conversion_mode")==0) unit_conversion_mode = true; else { @@ -214,8 +212,7 @@ void KimInit::determine_model_type_and_units(char * model_name, if (units_accepted) { logID = fmt::format("{}_Model", comm->me); KIM_Model_SetLogID(pkim, logID.c_str()); - *model_units = new char[strlen(user_units)+1]; - strcpy(*model_units,user_units); + *model_units = utils::strdup(user_units); return; } else if (unit_conversion_mode) { KIM_Model_Destroy(&pkim); @@ -237,8 +234,7 @@ void KimInit::determine_model_type_and_units(char * model_name, if (units_accepted) { logID = fmt::format("{}_Model", comm->me); KIM_Model_SetLogID(pkim, logID.c_str()); - *model_units = new char[strlen(systems[i])+1]; - strcpy(*model_units,systems[i]); + *model_units = utils::strdup(systems[i]); return; } KIM_Model_Destroy(&pkim); @@ -272,9 +268,7 @@ void KimInit::determine_model_type_and_units(char * model_name, if (0 == strcmp(sim_field,"units")) { KIM_SimulatorModel_GetSimulatorFieldLine( simulatorModel, i, 0, &sim_value); - int len=strlen(sim_value)+1; - *model_units = new char[len]; - strcpy(*model_units,sim_value); + *model_units = utils::strdup(sim_value); break; } } diff --git a/src/compute_coord_atom.cpp b/src/compute_coord_atom.cpp index 3055fb96ac..51a3618d5a 100644 --- a/src/compute_coord_atom.cpp +++ b/src/compute_coord_atom.cpp @@ -53,9 +53,7 @@ ComputeCoordAtom::ComputeCoordAtom(LAMMPS *lmp, int narg, char **arg) : int iarg = 5; if ((narg > 6) && (strcmp(arg[5],"group") == 0)) { - int len = strlen(arg[6])+1; - group2 = new char[len]; - strcpy(group2,arg[6]); + group2 = utils::strdup(arg[6]); iarg += 2; jgroup = group->find(group2); if (jgroup == -1) diff --git a/src/fix_property_atom.cpp b/src/fix_property_atom.cpp index 72f2eee605..c1c52a3f8c 100644 --- a/src/fix_property_atom.cpp +++ b/src/fix_property_atom.cpp @@ -13,13 +13,12 @@ #include "fix_property_atom.h" -#include #include "atom.h" #include "comm.h" -#include "memory.h" #include "error.h" +#include "memory.h" - +#include using namespace LAMMPS_NS; using namespace FixConst; @@ -126,9 +125,7 @@ FixPropertyAtom::FixPropertyAtom(LAMMPS *lmp, int narg, char **arg) : // store current atom style - int n = strlen(atom->atom_style) + 1; - astyle = new char[n]; - strcpy(astyle,atom->atom_style); + astyle = utils::strdup(atom->atom_style); // perform initial allocation of atom-based array // register with Atom class diff --git a/src/fix_setforce.cpp b/src/fix_setforce.cpp index 8430f4d784..7d41b428c9 100644 --- a/src/fix_setforce.cpp +++ b/src/fix_setforce.cpp @@ -49,9 +49,7 @@ FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) : xstr = ystr = zstr = nullptr; if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(&arg[3][2]) + 1; - xstr = new char[n]; - strcpy(xstr,&arg[3][2]); + xstr = utils::strdup(arg[3]+2); } else if (strcmp(arg[3],"NULL") == 0) { xstyle = NONE; } else { @@ -59,9 +57,7 @@ FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) : xstyle = CONSTANT; } if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(&arg[4][2]) + 1; - ystr = new char[n]; - strcpy(ystr,&arg[4][2]); + ystr = utils::strdup(arg[4]+2); } else if (strcmp(arg[4],"NULL") == 0) { ystyle = NONE; } else { @@ -69,9 +65,7 @@ FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) : ystyle = CONSTANT; } if (utils::strmatch(arg[5],"^v_")) { - int n = strlen(&arg[5][2]) + 1; - zstr = new char[n]; - strcpy(zstr,&arg[5][2]); + zstr = utils::strdup(arg[5]+2); } else if (strcmp(arg[5],"NULL") == 0) { zstyle = NONE; } else { @@ -91,9 +85,7 @@ FixSetForce::FixSetForce(LAMMPS *lmp, int narg, char **arg) : iregion = domain->find_region(arg[iarg+1]); if (iregion == -1) error->all(FLERR,"Region ID for fix setforce does not exist"); - int n = strlen(arg[iarg+1]) + 1; - idregion = new char[n]; - strcpy(idregion,arg[iarg+1]); + idregion = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix setforce command"); } diff --git a/src/fix_temp_berendsen.cpp b/src/fix_temp_berendsen.cpp index c80ffc7859..403e43cd61 100644 --- a/src/fix_temp_berendsen.cpp +++ b/src/fix_temp_berendsen.cpp @@ -12,20 +12,20 @@ ------------------------------------------------------------------------- */ #include "fix_temp_berendsen.h" -#include -#include #include "atom.h" -#include "force.h" #include "comm.h" -#include "input.h" -#include "variable.h" -#include "group.h" -#include "update.h" -#include "modify.h" #include "compute.h" #include "error.h" +#include "force.h" +#include "group.h" +#include "input.h" +#include "modify.h" +#include "update.h" +#include "variable.h" +#include +#include using namespace LAMMPS_NS; using namespace FixConst; @@ -52,9 +52,7 @@ FixTempBerendsen::FixTempBerendsen(LAMMPS *lmp, int narg, char **arg) : tstr = nullptr; if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(&arg[3][2]) + 1; - tstr = new char[n]; - strcpy(tstr,&arg[3][2]); + tstr = utils::strdup(arg[3]+2); tstyle = EQUAL; } else { t_start = utils::numeric(FLERR,arg[3],false,lmp); @@ -74,9 +72,7 @@ FixTempBerendsen::FixTempBerendsen(LAMMPS *lmp, int narg, char **arg) : // id = fix-ID + temp, compute group = fix group std::string cmd = id + std::string("_temp"); - id_temp = new char[cmd.size()+1]; - strcpy(id_temp,cmd.c_str()); - + id_temp = utils::strdup(cmd); cmd += fmt::format(" {} temp",group->names[igroup]); modify->add_compute(cmd); tflag = 1; diff --git a/src/fix_temp_csld.cpp b/src/fix_temp_csld.cpp index e38ad2a43f..f8afdada97 100644 --- a/src/fix_temp_csld.cpp +++ b/src/fix_temp_csld.cpp @@ -16,22 +16,22 @@ ------------------------------------------------------------------------- */ #include "fix_temp_csld.h" -#include + +#include "atom.h" +#include "comm.h" +#include "compute.h" +#include "error.h" +#include "force.h" +#include "group.h" +#include "input.h" +#include "memory.h" +#include "modify.h" +#include "random_mars.h" +#include "update.h" +#include "variable.h" #include -#include "atom.h" -#include "force.h" -#include "memory.h" -#include "comm.h" -#include "input.h" -#include "variable.h" -#include "group.h" -#include "update.h" -#include "modify.h" -#include "compute.h" -#include "random_mars.h" -#include "error.h" - +#include using namespace LAMMPS_NS; using namespace FixConst; @@ -58,9 +58,7 @@ FixTempCSLD::FixTempCSLD(LAMMPS *lmp, int narg, char **arg) : tstr = nullptr; if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(&arg[3][2]) + 1; - tstr = new char[n]; - strcpy(tstr,&arg[3][2]); + tstr = utils::strdup(arg[3]+2); tstyle = EQUAL; } else { t_start = utils::numeric(FLERR,arg[3],false,lmp); @@ -83,9 +81,7 @@ FixTempCSLD::FixTempCSLD(LAMMPS *lmp, int narg, char **arg) : // id = fix-ID + temp, compute group = fix group std::string cmd = id + std::string("_temp"); - id_temp = new char[cmd.size()+1]; - strcpy(id_temp,cmd.c_str()); - + id_temp = utils::strdup(cmd); cmd += fmt::format(" {} temp",group->names[igroup]); modify->add_compute(cmd); tflag = 1; diff --git a/src/fix_temp_csvr.cpp b/src/fix_temp_csvr.cpp index 9c10b0fb7d..3f71c211e5 100644 --- a/src/fix_temp_csvr.cpp +++ b/src/fix_temp_csvr.cpp @@ -18,22 +18,21 @@ #include "fix_temp_csvr.h" +#include "atom.h" +#include "comm.h" +#include "compute.h" +#include "error.h" +#include "force.h" +#include "group.h" +#include "input.h" +#include "modify.h" +#include "random_mars.h" +#include "update.h" +#include "variable.h" + #include #include -#include "atom.h" -#include "force.h" -#include "comm.h" -#include "input.h" -#include "variable.h" -#include "group.h" -#include "update.h" -#include "modify.h" -#include "compute.h" -#include "random_mars.h" -#include "error.h" - - using namespace LAMMPS_NS; using namespace FixConst; @@ -135,9 +134,7 @@ FixTempCSVR::FixTempCSVR(LAMMPS *lmp, int narg, char **arg) : tstr = nullptr; if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(&arg[3][2]) + 1; - tstr = new char[n]; - strcpy(tstr,&arg[3][2]); + tstr = utils::strdup(arg[3]+2); tstyle = EQUAL; } else { t_start = utils::numeric(FLERR,arg[3],false,lmp); @@ -160,9 +157,7 @@ FixTempCSVR::FixTempCSVR(LAMMPS *lmp, int narg, char **arg) : // id = fix-ID + temp, compute group = fix group std::string cmd = id + std::string("_temp"); - id_temp = new char[cmd.size()+1]; - strcpy(id_temp,cmd.c_str()); - + id_temp = utils::strdup(cmd); cmd += fmt::format(" {} temp",group->names[igroup]); modify->add_compute(cmd); tflag = 1; diff --git a/src/fix_temp_rescale.cpp b/src/fix_temp_rescale.cpp index 321486b761..6ad0d705f6 100644 --- a/src/fix_temp_rescale.cpp +++ b/src/fix_temp_rescale.cpp @@ -12,20 +12,20 @@ ------------------------------------------------------------------------- */ #include "fix_temp_rescale.h" -#include -#include #include "atom.h" -#include "force.h" -#include "group.h" -#include "update.h" #include "comm.h" -#include "input.h" -#include "variable.h" -#include "modify.h" #include "compute.h" #include "error.h" +#include "force.h" +#include "group.h" +#include "input.h" +#include "modify.h" +#include "update.h" +#include "variable.h" +#include +#include using namespace LAMMPS_NS; using namespace FixConst; @@ -52,9 +52,7 @@ FixTempRescale::FixTempRescale(LAMMPS *lmp, int narg, char **arg) : tstr = nullptr; if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(&arg[4][2]) + 1; - tstr = new char[n]; - strcpy(tstr,&arg[4][2]); + tstr = utils::strdup(arg[4]+2); tstyle = EQUAL; } else { t_start = utils::numeric(FLERR,arg[4],false,lmp); @@ -70,9 +68,7 @@ FixTempRescale::FixTempRescale(LAMMPS *lmp, int narg, char **arg) : // id = fix-ID + temp, compute group = fix group std::string cmd = id + std::string("_temp"); - id_temp = new char[cmd.size()+1]; - strcpy(id_temp,cmd.c_str()); - + id_temp = utils::strdup(cmd); cmd += fmt::format(" {} temp",group->names[igroup]); modify->add_compute(cmd); tflag = 1; diff --git a/src/fix_wall.cpp b/src/fix_wall.cpp index 255f851408..26ea22db9d 100644 --- a/src/fix_wall.cpp +++ b/src/fix_wall.cpp @@ -81,18 +81,14 @@ FixWall::FixWall(LAMMPS *lmp, int narg, char **arg) : else coord0[nwall] = domain->boxhi[dim]; } else if (utils::strmatch(arg[iarg+1],"^v_")) { xstyle[nwall] = VARIABLE; - int n = strlen(&arg[iarg+1][2]) + 1; - xstr[nwall] = new char[n]; - strcpy(xstr[nwall],&arg[iarg+1][2]); + xstr[nwall] = utils::strdup(arg[iarg+1]+2); } else { xstyle[nwall] = CONSTANT; coord0[nwall] = utils::numeric(FLERR,arg[iarg+1],false,lmp); } if (utils::strmatch(arg[iarg+2],"^v_")) { - int n = strlen(&arg[iarg+2][2]) + 1; - estr[nwall] = new char[n]; - strcpy(estr[nwall],&arg[iarg+2][2]); + estr[nwall] = utils::strdup(arg[iarg+2]+2); estyle[nwall] = VARIABLE; } else { epsilon[nwall] = utils::numeric(FLERR,arg[iarg+2],false,lmp); @@ -101,9 +97,7 @@ FixWall::FixWall(LAMMPS *lmp, int narg, char **arg) : if (utils::strmatch(style,"^wall/morse")) { if (utils::strmatch(arg[iarg+3],"^v_")) { - int n = strlen(&arg[iarg+3][2]) + 1; - astr[nwall] = new char[n]; - strcpy(astr[nwall],&arg[iarg+3][2]); + astr[nwall] = utils::strdup(arg[iarg+3]+2); astyle[nwall] = VARIABLE; } else { alpha[nwall] = utils::numeric(FLERR,arg[iarg+3],false,lmp); @@ -113,9 +107,7 @@ FixWall::FixWall(LAMMPS *lmp, int narg, char **arg) : } if (utils::strmatch(arg[iarg+3],"^v_")) { - int n = strlen(&arg[iarg+3][2]) + 1; - sstr[nwall] = new char[n]; - strcpy(sstr[nwall],&arg[iarg+3][2]); + sstr[nwall] = utils::strdup(arg[iarg+3]+2); sstyle[nwall] = VARIABLE; } else { sigma[nwall] = utils::numeric(FLERR,arg[iarg+3],false,lmp); diff --git a/src/fix_wall_reflect.cpp b/src/fix_wall_reflect.cpp index 12dd7b5877..77c3cd368f 100644 --- a/src/fix_wall_reflect.cpp +++ b/src/fix_wall_reflect.cpp @@ -75,9 +75,7 @@ FixWallReflect::FixWallReflect(LAMMPS *lmp, int narg, char **arg) : else coord0[nwall] = domain->boxhi[dim]; } else if (utils::strmatch(arg[iarg+1],"^v_")) { wallstyle[nwall] = VARIABLE; - int n = strlen(&arg[iarg+1][2]) + 1; - varstr[nwall] = new char[n]; - strcpy(varstr[nwall],&arg[iarg+1][2]); + varstr[nwall] = utils::strdup(arg[iarg+1]+2); } else { wallstyle[nwall] = CONSTANT; coord0[nwall] = utils::numeric(FLERR,arg[iarg+1],false,lmp); diff --git a/src/output.cpp b/src/output.cpp index 31377e9ca6..828796b873 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -665,9 +665,7 @@ void Output::set_thermo(int narg, char **arg) var_thermo = nullptr; if (utils::strmatch(arg[0],"^v_")) { - int n = strlen(&arg[0][2]) + 1; - var_thermo = new char[n]; - strcpy(var_thermo,&arg[0][2]); + var_thermo = utils::strdup(arg[0]+2); } else { thermo_every = utils::inumeric(FLERR,arg[0],false,lmp); if (thermo_every < 0) error->all(FLERR,"Illegal thermo command"); @@ -745,9 +743,7 @@ void Output::create_restart(int narg, char **arg) if (varflag) { delete [] var_restart_single; - int n = strlen(&arg[0][2]) + 1; - var_restart_single = new char[n]; - strcpy(var_restart_single,&arg[0][2]); + var_restart_single = utils::strdup(arg[0]+2); restart_every_single = 0; } else restart_every_single = every; @@ -763,9 +759,7 @@ void Output::create_restart(int narg, char **arg) if (varflag) { delete [] var_restart_double; - int n = strlen(&arg[0][2]) + 1; - var_restart_double = new char[n]; - strcpy(var_restart_double,&arg[0][2]); + var_restart_double = utils::strdup(arg[0]+2); restart_every_double = 0; } else restart_every_double = every; diff --git a/src/region_cylinder.cpp b/src/region_cylinder.cpp index 6aec067a40..2092a03b79 100644 --- a/src/region_cylinder.cpp +++ b/src/region_cylinder.cpp @@ -45,9 +45,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : if (axis == 'x') { if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(arg[3]+2) + 1; - c1str = new char[n]; - strcpy(c1str,arg[3]+2); + c1str = utils::strdup(arg[3]+2); c1 = 0.0; c1style = VARIABLE; varshape = 1; @@ -56,9 +54,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : c1style = CONSTANT; } if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(arg[4]+2) + 1; - c2str = new char[n]; - strcpy(c2str,arg[4]+2); + c2str = utils::strdup(arg[4]+2); c2 = 0.0; c2style = VARIABLE; varshape = 1; @@ -68,9 +64,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : } } else if (axis == 'y') { if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(arg[3]+2) + 1; - c1str = new char[n]; - strcpy(c1str,arg[3]+2); + c1str = utils::strdup(arg[3]+2); c1 = 0.0; c1style = VARIABLE; varshape = 1; @@ -79,9 +73,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : c1style = CONSTANT; } if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(arg[4]+2) + 1; - c2str = new char[n]; - strcpy(c2str,arg[4]+2); + c2str = utils::strdup(arg[4]+2); c2 = 0.0; c2style = VARIABLE; varshape = 1; @@ -91,9 +83,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : } } else if (axis == 'z') { if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(arg[3]+2) + 1; - c1str = new char[n]; - strcpy(c1str,arg[3]+2); + c1str = utils::strdup(arg[3]+2); c1 = 0.0; c1style = VARIABLE; varshape = 1; @@ -102,9 +92,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : c1style = CONSTANT; } if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(arg[4]+2) + 1; - c2str = new char[n]; - strcpy(c2str,arg[4]+2); + c2str = utils::strdup(arg[4]+2); c2 = 0.0; c2style = VARIABLE; varshape = 1; @@ -115,9 +103,7 @@ RegCylinder::RegCylinder(LAMMPS *lmp, int narg, char **arg) : } if (utils::strmatch(arg[5],"^v_")) { - int n = strlen(&arg[5][2]) + 1; - rstr = new char[n]; - strcpy(rstr,&arg[5][2]); + rstr = utils::strdup(arg[5]+2); radius = 0.0; rstyle = VARIABLE; varshape = 1; diff --git a/src/region_sphere.cpp b/src/region_sphere.cpp index a5fc292b41..8921cbd456 100644 --- a/src/region_sphere.cpp +++ b/src/region_sphere.cpp @@ -33,9 +33,7 @@ RegSphere::RegSphere(LAMMPS *lmp, int narg, char **arg) : options(narg-6,&arg[6]); if (utils::strmatch(arg[2],"^v_")) { - int n = strlen(arg[2]+2) + 1; - xstr = new char[n]; - strcpy(xstr,arg[2]+2); + xstr = utils::strdup(arg[2]+2); xc = 0.0; xstyle = VARIABLE; varshape = 1; @@ -45,9 +43,7 @@ RegSphere::RegSphere(LAMMPS *lmp, int narg, char **arg) : } if (utils::strmatch(arg[3],"^v_")) { - int n = strlen(arg[3]+2) + 1; - ystr = new char[n]; - strcpy(ystr,arg[3]+2); + ystr = utils::strdup(arg[3]+2); yc = 0.0; ystyle = VARIABLE; varshape = 1; @@ -57,9 +53,7 @@ RegSphere::RegSphere(LAMMPS *lmp, int narg, char **arg) : } if (utils::strmatch(arg[4],"^v_")) { - int n = strlen(arg[4]+2) + 1; - zstr = new char[n]; - strcpy(zstr,arg[4]+2); + zstr = utils::strdup(arg[4]+2); zc = 0.0; zstyle = VARIABLE; varshape = 1; @@ -69,9 +63,7 @@ RegSphere::RegSphere(LAMMPS *lmp, int narg, char **arg) : } if (utils::strmatch(arg[5],"^v_")) { - int n = strlen(&arg[5][2]) + 1; - rstr = new char[n]; - strcpy(rstr,&arg[5][2]); + rstr = utils::strdup(arg[5]+2); radius = 0.0; rstyle = VARIABLE; varshape = 1; diff --git a/src/set.cpp b/src/set.cpp index 4d27e8183b..fdc0c37bfd 100644 --- a/src/set.cpp +++ b/src/set.cpp @@ -71,9 +71,7 @@ void Set::command(int narg, char **arg) else if (strcmp(arg[0],"region") == 0) style = REGION_SELECT; else error->all(FLERR,"Illegal set command"); - int n = strlen(arg[1]) + 1; - id = new char[n]; - strcpy(id,arg[1]); + id = utils::strdup(arg[1]); select = nullptr; selection(atom->nlocal);