diff --git a/src/EXTRA-FIX/fix_ave_correlate_long.cpp b/src/EXTRA-FIX/fix_ave_correlate_long.cpp index 32a304d08f..6e4e26754f 100644 --- a/src/EXTRA-FIX/fix_ave_correlate_long.cpp +++ b/src/EXTRA-FIX/fix_ave_correlate_long.cpp @@ -156,18 +156,14 @@ FixAveCorrelateLong::FixAveCorrelateLong(LAMMPS * lmp, int narg, char **arg): } else if (strcmp(arg[iarg],"title1") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/correlate/long command"); - delete [] title1; - int n = strlen(arg[iarg+1]) + 1; - title1 = new char[n]; - strcpy(title1,arg[iarg+1]); + delete[] title1; + title1 = utils::strdup(arg[iarg+1]); iarg += 2; } else if (strcmp(arg[iarg],"title2") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix ave/correlate/long command"); - delete [] title2; - int n = strlen(arg[iarg+1]) + 1; - title2 = new char[n]; - strcpy(title2,arg[iarg+1]); + delete[] title2; + title2 = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal fix ave/correlate/long command"); } diff --git a/src/EXTRA-FIX/fix_numdiff.cpp b/src/EXTRA-FIX/fix_numdiff.cpp index dbe2f0a9dc..4c9c08b681 100644 --- a/src/EXTRA-FIX/fix_numdiff.cpp +++ b/src/EXTRA-FIX/fix_numdiff.cpp @@ -57,8 +57,7 @@ FixNumDiff::FixNumDiff(LAMMPS *lmp, int narg, char **arg) : error->all(FLERR,"Illegal fix numdiff command"); std::string cmd = id + std::string("_pe"); - id_pe = new char[cmd.size()+1]; - strcpy(id_pe,cmd.c_str()); + id_pe = utils::strdup(cmd); cmd += " all pe"; modify->add_compute(cmd); diff --git a/src/GPU/pair_eam_alloy_gpu.cpp b/src/GPU/pair_eam_alloy_gpu.cpp index 9c4ee76f15..e363ca670d 100644 --- a/src/GPU/pair_eam_alloy_gpu.cpp +++ b/src/GPU/pair_eam_alloy_gpu.cpp @@ -394,12 +394,8 @@ void PairEAMAlloyGPU::read_file(char *filename) error->one(FLERR,"Incorrect element names in EAM potential file"); file->elements = new char*[file->nelements]; - for (int i = 0; i < file->nelements; i++) { - const std::string word = values.next_string(); - const int n = word.length() + 1; - file->elements[i] = new char[n]; - strcpy(file->elements[i], word.c_str()); - } + for (int i = 0; i < file->nelements; i++) + file->elements[i] = utils::strdup(values.next_string()); // diff --git a/src/GPU/pair_eam_fs_gpu.cpp b/src/GPU/pair_eam_fs_gpu.cpp index b4c14d62cd..6e463476d7 100644 --- a/src/GPU/pair_eam_fs_gpu.cpp +++ b/src/GPU/pair_eam_fs_gpu.cpp @@ -395,9 +395,7 @@ void PairEAMFSGPU::read_file(char *filename) file->elements = new char*[file->nelements]; for (int i = 0; i < file->nelements; i++) { const std::string word = values.next_string(); - const int n = word.length() + 1; - file->elements[i] = new char[n]; - strcpy(file->elements[i], word.c_str()); + file->elements[i] = utils::strdup(word); } // @@ -408,6 +406,7 @@ void PairEAMFSGPU::read_file(char *filename) file->nr = values.next_int(); file->dr = values.next_double(); file->cut = values.next_double(); + rhomax = 0.0; if ((file->nrho <= 0) || (file->nr <= 0) || (file->dr <= 0.0)) error->one(FLERR,"Invalid EAM potential file"); diff --git a/src/GRANULAR/compute_fabric.cpp b/src/GRANULAR/compute_fabric.cpp index 341e32b867..3e7754ce8c 100644 --- a/src/GRANULAR/compute_fabric.cpp +++ b/src/GRANULAR/compute_fabric.cpp @@ -37,7 +37,7 @@ enum { CN, BR, FN, FT }; /* ---------------------------------------------------------------------- */ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) : - Compute(lmp, narg, arg), tensor_style(NULL) + Compute(lmp, narg, arg), tensor_style(nullptr) { if (narg < 3) error->all(FLERR, "Illegal compute fabric command"); @@ -80,9 +80,8 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) : if (iarg + 1 >= narg) error->all(FLERR, "Invalid keyword in compute fabric command"); int ntypes = atom->ntypes; - int i, j, itype, jtype, in, jn, infield, jnfield; + int i, j, itype, jtype; int inlo, inhi, jnlo, jnhi; - char *istr, *jstr; if (!type_filter) { memory->create(type_filter, ntypes + 1, ntypes + 1, "compute/fabric:type_filter"); @@ -91,24 +90,12 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) : } } - in = strlen(arg[iarg + 1]) + 1; - istr = new char[in]; - strcpy(istr, arg[iarg + 1]); - std::vector iwords = Tokenizer(istr, ",").as_vector(); - infield = iwords.size(); - - jn = strlen(arg[iarg + 2]) + 1; - jstr = new char[jn]; - strcpy(jstr, arg[iarg + 2]); - std::vector jwords = Tokenizer(jstr, ",").as_vector(); - jnfield = jwords.size(); - - for (i = 0; i < infield; i++) { - const char *ifield = iwords[i].c_str(); + std::vector iwords = Tokenizer(arg[iarg+1], ",").as_vector(); + std::vector jwords = Tokenizer(arg[iarg+2], ",").as_vector(); + for (const auto &ifield : iwords) { utils::bounds(FLERR, ifield, 1, ntypes, inlo, inhi, error); - for (j = 0; j < jnfield; j++) { - const char *jfield = jwords[j].c_str(); + for (const auto &jfield : jwords) { utils::bounds(FLERR, jfield, 1, ntypes, jnlo, jnhi, error); for (itype = inlo; itype <= inhi; itype++) { @@ -119,10 +106,6 @@ ComputeFabric::ComputeFabric(LAMMPS *lmp, int narg, char **arg) : } } } - - delete[] istr; - delete[] jstr; - iarg += 2; } else error->all(FLERR, "Illegal compute fabric command"); @@ -152,7 +135,7 @@ ComputeFabric::~ComputeFabric() void ComputeFabric::init() { - if (force->pair == NULL) error->all(FLERR, "No pair style is defined for compute fabric"); + if (force->pair == nullptr) error->all(FLERR, "No pair style is defined for compute fabric"); if (force->pair->single_enable == 0 && (fn_flag || ft_flag)) error->all(FLERR, "Pair style does not support compute fabric normal or tangential force"); diff --git a/src/GRANULAR/fix_wall_gran_region.cpp b/src/GRANULAR/fix_wall_gran_region.cpp index 37afe9632c..45905aea1c 100644 --- a/src/GRANULAR/fix_wall_gran_region.cpp +++ b/src/GRANULAR/fix_wall_gran_region.cpp @@ -46,8 +46,7 @@ FixWallGranRegion::FixWallGranRegion(LAMMPS *lmp, int narg, char **arg) : if (iregion == -1) error->all(FLERR,"Region ID for fix wall/gran/region does not exist"); region = domain->regions[iregion]; - region_style = new char[strlen(region->style)+1]; - strcpy(region_style,region->style); + region_style = utils::strdup(region->style); nregion = region->nregion; tmax = domain->regions[iregion]->tmax; diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index 95faca893c..3e207baae7 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -54,8 +54,7 @@ FixHalt::FixHalt(LAMMPS *lmp, int narg, char **arg) : attribute = TLIMIT; } else if (strcmp(arg[iarg],"diskfree") == 0) { attribute = DISKFREE; - dlimit_path = new char[2]; - strcpy(dlimit_path,"."); + dlimit_path = utils::strdup("."); } else if (strcmp(arg[iarg],"bondmax") == 0) { attribute = BONDMAX; } else { diff --git a/src/force.cpp b/src/force.cpp index 0f5d49e9c8..c7dc198e83 100644 --- a/src/force.cpp +++ b/src/force.cpp @@ -737,9 +737,7 @@ void Force::store_style(char *&str, const std::string &style, int sflag) if (sflag == 1) estyle += std::string("/") + lmp->suffix; else if (sflag == 2) estyle += std::string("/") + lmp->suffix2; else if (sflag == 3) estyle += std::string("/") + lmp->suffixp; - - str = new char[estyle.size()+1]; - strcpy(str,estyle.c_str()); + str = utils::strdup(estyle); } /* ---------------------------------------------------------------------- diff --git a/src/lammps.cpp b/src/lammps.cpp index b00181452b..291bb84499 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -412,17 +412,11 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if (strcmp(arg[iarg+1],"hybrid") == 0) { if (iarg+4 > narg) error->universe_all(FLERR,"Invalid command-line argument"); - int n = strlen(arg[iarg+2]) + 1; - suffix = new char[n]; - strcpy(suffix,arg[iarg+2]); - n = strlen(arg[iarg+3]) + 1; - suffix2 = new char[n]; - strcpy(suffix2,arg[iarg+3]); + suffix = utils::strdup(arg[iarg+2]); + suffix2 = utils::strdup(arg[iarg+3]); iarg += 4; } else { - int n = strlen(arg[iarg+1]) + 1; - suffix = new char[n]; - strcpy(suffix,arg[iarg+1]); + suffix = utils::strdup(arg[iarg+1]); iarg += 2; } @@ -870,14 +864,12 @@ void LAMMPS::post_create() // invoke any command-line package commands if (num_package) { - char str[256]; + std::string str; for (int i = 0; i < num_package; i++) { - strcpy(str,"package"); + str = "package"; for (char **ptr = packargs[i]; *ptr != nullptr; ++ptr) { - if (strlen(str) + strlen(*ptr) + 2 > 256) - error->all(FLERR,"Too many -pk arguments in command line"); - strcat(str," "); - strcat(str,*ptr); + str += " "; + str += *ptr; } input->one(str); } diff --git a/src/neighbor.cpp b/src/neighbor.cpp index 6c25332257..d6fc0ac4dc 100644 --- a/src/neighbor.cpp +++ b/src/neighbor.cpp @@ -1887,11 +1887,12 @@ int Neighbor::choose_pair(NeighRequest *rq) // convert newton request to newtflag = on or off - int newtflag; - if (rq->newton == 0 && newton_pair) newtflag = 1; - else if (rq->newton == 0 && !newton_pair) newtflag = 0; - else if (rq->newton == 1) newtflag = 1; - else if (rq->newton == 2) newtflag = 0; + bool newtflag; + if (rq->newton == 0 && newton_pair) newtflag = true; + else if (rq->newton == 0 && !newton_pair) newtflag = false; + else if (rq->newton == 1) newtflag = true; + else if (rq->newton == 2) newtflag = false; + else error->all(FLERR,"Illegal 'newton' flag in neighbor list request"); int molecular = atom->molecular; @@ -2565,27 +2566,16 @@ void Neighbor::modify_params(int narg, char **arg) type2collection[i] = -1; // For each custom range, define mapping for types in interval - int nfield; - char *str; for (i = 0; i < ncollections; i++){ - n = strlen(arg[iarg+2+i]) + 1; - str = new char[n]; - strcpy(str,arg[iarg+2+i]); - std::vector words = Tokenizer(str, ",").as_vector(); - nfield = words.size(); - - for (j = 0; j < nfield; j++) { - const char * field = words[j].c_str(); - utils::bounds(FLERR,field,1,ntypes,nlo,nhi,error); - + std::vector words = Tokenizer(arg[iarg+2+i], ",").as_vector(); + for (const auto &word : words) { + utils::bounds(FLERR,word,1,ntypes,nlo,nhi,error); for (k = nlo; k <= nhi; k++) { if (type2collection[k] != -1) error->all(FLERR,"Type specified more than once in collection/type commnd"); type2collection[k] = i; } } - - delete [] str; } // Check for undefined atom type diff --git a/src/update.cpp b/src/update.cpp index 7f14597e7e..72b60c3d3b 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -335,8 +335,7 @@ void Update::create_integrate(int narg, char **arg, int trysuffix) if (sflag == 1) estyle += lmp->suffix; else estyle += lmp->suffix2; } - integrate_style = new char[estyle.size()+1]; - strcpy(integrate_style,estyle.c_str()); + integrate_style = utils::strdup(estyle); } /* ---------------------------------------------------------------------- @@ -406,8 +405,7 @@ void Update::create_minimize(int narg, char **arg, int trysuffix) if (sflag == 1) estyle += lmp->suffix; else estyle += lmp->suffix2; } - minimize_style = new char[estyle.size()+1]; - strcpy(minimize_style,estyle.c_str()); + minimize_style = utils::strdup(estyle); } /* ----------------------------------------------------------------------