diff --git a/src/atom.cpp b/src/atom.cpp index 959bf39ddb..a4fe74e6dd 100644 --- a/src/atom.cpp +++ b/src/atom.cpp @@ -341,11 +341,8 @@ void Atom::settings(Atom *old) map_style = old->map_style; sortfreq = old->sortfreq; userbinsize = old->userbinsize; - if (old->firstgroupname) { - int n = strlen(old->firstgroupname) + 1; - firstgroupname = new char[n]; - strcpy(firstgroupname,old->firstgroupname); - } + if (old->firstgroupname) + firstgroupname = utils::strdup(old->firstgroupname); } /* ---------------------------------------------------------------------- @@ -532,9 +529,7 @@ void Atom::add_peratom(const char *name, void *address, memory->srealloc(peratom,maxperatom*sizeof(PerAtom),"atom:peratom"); } - int n = strlen(name) + 1; - peratom[nperatom].name = new char[n]; - strcpy(peratom[nperatom].name,name); + peratom[nperatom].name = utils::strdup(name); peratom[nperatom].address = address; peratom[nperatom].datatype = datatype; peratom[nperatom].cols = cols; @@ -582,9 +577,7 @@ void Atom::add_peratom_vary(const char *name, void *address, memory->srealloc(peratom,maxperatom*sizeof(PerAtom),"atom:peratom"); } - int n = strlen(name) + 1; - peratom[nperatom].name = new char[n]; - strcpy(peratom[nperatom].name,name); + peratom[nperatom].name = utils::strdup(name); peratom[nperatom].address = address; peratom[nperatom].datatype = datatype; peratom[nperatom].cols = -1; @@ -815,9 +808,7 @@ void Atom::modify_params(int narg, char **arg) delete [] firstgroupname; firstgroupname = nullptr; } else { - int n = strlen(arg[iarg+1]) + 1; - firstgroupname = new char[n]; - strcpy(firstgroupname,arg[iarg+1]); + firstgroupname = utils::strdup(arg[iarg+1]); sortfreq = 0; } iarg += 2; @@ -2427,9 +2418,7 @@ int Atom::add_custom(const char *name, int flag) nivector++; iname = (char **) memory->srealloc(iname,nivector*sizeof(char *), "atom:iname"); - int n = strlen(name) + 1; - iname[index] = new char[n]; - strcpy(iname[index],name); + iname[index] = utils::strdup(name); ivector = (int **) memory->srealloc(ivector,nivector*sizeof(int *), "atom:ivector"); memory->create(ivector[index],nmax,"atom:ivector"); @@ -2438,9 +2427,7 @@ int Atom::add_custom(const char *name, int flag) ndvector++; dname = (char **) memory->srealloc(dname,ndvector*sizeof(char *), "atom:dname"); - int n = strlen(name) + 1; - dname[index] = new char[n]; - strcpy(dname[index],name); + dname[index] = utils::strdup(name); dvector = (double **) memory->srealloc(dvector,ndvector*sizeof(double *), "atom:dvector"); memory->create(dvector[index],nmax,"atom:dvector"); diff --git a/src/atom_vec.cpp b/src/atom_vec.cpp index 4406ac5f23..7d7cfa4878 100644 --- a/src/atom_vec.cpp +++ b/src/atom_vec.cpp @@ -154,11 +154,8 @@ void AtomVec::store_args(int narg, char **arg) nargcopy = narg; if (nargcopy) argcopy = new char*[nargcopy]; else argcopy = nullptr; - for (int i = 0; i < nargcopy; i++) { - int n = strlen(arg[i]) + 1; - argcopy[i] = new char[n]; - strcpy(argcopy[i],arg[i]); - } + for (int i = 0; i < nargcopy; i++) + argcopy[i] = utils::strdup(arg[i]); } /* ---------------------------------------------------------------------- diff --git a/src/atom_vec_hybrid.cpp b/src/atom_vec_hybrid.cpp index 8679b7383d..6ddb72a1bf 100644 --- a/src/atom_vec_hybrid.cpp +++ b/src/atom_vec_hybrid.cpp @@ -109,8 +109,7 @@ void AtomVecHybrid::process_args(int narg, char **arg) if (strcmp(arg[iarg],keywords[i]) == 0) error->all(FLERR,"Atom style hybrid cannot use same atom style twice"); styles[nstyles] = atom->new_avec(arg[iarg],1,dummy); - keywords[nstyles] = new char[strlen(arg[iarg])+1]; - strcpy(keywords[nstyles],arg[iarg]); + keywords[nstyles] = utils::strdup(arg[iarg]); jarg = iarg + 1; while (jarg < narg && !known_style(arg[jarg])) jarg++; styles[nstyles]->process_args(jarg-iarg-1,&arg[iarg+1]); @@ -601,13 +600,10 @@ void AtomVecHybrid::build_styles() allstyles = new char*[nallstyles]; - int n; nallstyles = 0; #define ATOM_CLASS -#define AtomStyle(key,Class) \ - n = strlen(#key) + 1; \ - allstyles[nallstyles] = new char[n]; \ - strcpy(allstyles[nallstyles],#key); \ +#define AtomStyle(key,Class) \ + allstyles[nallstyles] = utils::strdup(#key); \ nallstyles++; #include "style_atom.h" // IWYU pragma: keep #undef AtomStyle diff --git a/src/body.cpp b/src/body.cpp index 35d6e53753..89bb2fc1d9 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -20,9 +20,7 @@ using namespace LAMMPS_NS; Body::Body(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) { - int n = strlen(arg[0]) + 1; - style = new char[n]; - strcpy(style,arg[0]); + style = utils::strdup(arg[0]); icp = nullptr; dcp = nullptr; } diff --git a/src/comm.cpp b/src/comm.cpp index 8e119744fc..6d3f72d9c0 100644 --- a/src/comm.cpp +++ b/src/comm.cpp @@ -149,16 +149,11 @@ void Comm::copy_arrays(Comm *oldcomm) memcpy(cutusermulti,oldcomm->cutusermulti,atom->ntypes+1); } - if (customfile) { - int n = strlen(oldcomm->customfile) + 1; - customfile = new char[n]; - strcpy(customfile,oldcomm->customfile); - } - if (outfile) { - int n = strlen(oldcomm->outfile) + 1; - outfile = new char[n]; - strcpy(outfile,oldcomm->outfile); - } + if (customfile) + customfile = utils::strdup(oldcomm->customfile); + + if (outfile) + outfile = utils::strdup(oldcomm->outfile); } /* ---------------------------------------------------------------------- @@ -391,9 +386,7 @@ void Comm::set_processors(int narg, char **arg) if (iarg+3 > narg) error->all(FLERR,"Illegal processors command"); gridflag = CUSTOM; delete [] customfile; - int n = strlen(arg[iarg+2]) + 1; - customfile = new char[n]; - strcpy(customfile,arg[iarg+2]); + customfile = utils::strdup(arg[iarg+2]); iarg += 1; } else error->all(FLERR,"Illegal processors command"); @@ -453,9 +446,7 @@ void Comm::set_processors(int narg, char **arg) } else if (strcmp(arg[iarg],"file") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal processors command"); delete [] outfile; - int n = strlen(arg[iarg+1]) + 1; - outfile = new char[n]; - strcpy(outfile,arg[iarg+1]); + outfile = utils::strdup(arg[iarg+1]); iarg += 2; } else error->all(FLERR,"Illegal processors command"); diff --git a/src/utils.cpp b/src/utils.cpp index d34e5794ab..66bc053542 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -554,9 +554,7 @@ int utils::expand_args(const char *file, int line, int narg, char **arg, earg = (char **) lmp->memory->srealloc(earg,maxarg*sizeof(char *),"input:earg"); } - n = strlen(arg[iarg]) + 1; - earg[newarg] = new char[n]; - strcpy(earg[newarg],arg[iarg]); + earg[newarg] = utils::strdup(arg[iarg]); newarg++; } }