diff --git a/src/COMPRESS/dump_atom_gz.cpp b/src/COMPRESS/dump_atom_gz.cpp index 7c30d7c101..f252e32064 100644 --- a/src/COMPRESS/dump_atom_gz.cpp +++ b/src/COMPRESS/dump_atom_gz.cpp @@ -77,14 +77,12 @@ void DumpAtomGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_atom_zstd.cpp b/src/COMPRESS/dump_atom_zstd.cpp index f51a8b393b..3dde07bbf4 100644 --- a/src/COMPRESS/dump_atom_zstd.cpp +++ b/src/COMPRESS/dump_atom_zstd.cpp @@ -76,14 +76,12 @@ void DumpAtomZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_cfg_gz.cpp b/src/COMPRESS/dump_cfg_gz.cpp index 9beb606b24..378baf502f 100644 --- a/src/COMPRESS/dump_cfg_gz.cpp +++ b/src/COMPRESS/dump_cfg_gz.cpp @@ -81,14 +81,12 @@ void DumpCFGGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_cfg_zstd.cpp b/src/COMPRESS/dump_cfg_zstd.cpp index 13890334ba..459649c70a 100644 --- a/src/COMPRESS/dump_cfg_zstd.cpp +++ b/src/COMPRESS/dump_cfg_zstd.cpp @@ -79,14 +79,12 @@ void DumpCFGZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_custom_gz.cpp b/src/COMPRESS/dump_custom_gz.cpp index f345f4b111..4f03a4a232 100644 --- a/src/COMPRESS/dump_custom_gz.cpp +++ b/src/COMPRESS/dump_custom_gz.cpp @@ -79,14 +79,12 @@ void DumpCustomGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_custom_zstd.cpp b/src/COMPRESS/dump_custom_zstd.cpp index 81ace6172f..3aa3f874ea 100644 --- a/src/COMPRESS/dump_custom_zstd.cpp +++ b/src/COMPRESS/dump_custom_zstd.cpp @@ -76,14 +76,12 @@ void DumpCustomZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_local_gz.cpp b/src/COMPRESS/dump_local_gz.cpp index e0d2f0d2dd..e8065a848a 100644 --- a/src/COMPRESS/dump_local_gz.cpp +++ b/src/COMPRESS/dump_local_gz.cpp @@ -79,14 +79,12 @@ void DumpLocalGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_local_zstd.cpp b/src/COMPRESS/dump_local_zstd.cpp index c03670ffba..d26555d282 100644 --- a/src/COMPRESS/dump_local_zstd.cpp +++ b/src/COMPRESS/dump_local_zstd.cpp @@ -78,14 +78,12 @@ void DumpLocalZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_xyz_gz.cpp b/src/COMPRESS/dump_xyz_gz.cpp index 21a55f6b07..c63d354e80 100644 --- a/src/COMPRESS/dump_xyz_gz.cpp +++ b/src/COMPRESS/dump_xyz_gz.cpp @@ -78,14 +78,12 @@ void DumpXYZGZ::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/COMPRESS/dump_xyz_zstd.cpp b/src/COMPRESS/dump_xyz_zstd.cpp index 9c220bdca7..7c5b73d0ba 100644 --- a/src/COMPRESS/dump_xyz_zstd.cpp +++ b/src/COMPRESS/dump_xyz_zstd.cpp @@ -76,14 +76,12 @@ void DumpXYZZstd::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } diff --git a/src/dump.cpp b/src/dump.cpp index ab2a84ab22..031364dada 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -46,20 +46,14 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) MPI_Comm_rank(world,&me); MPI_Comm_size(world,&nprocs); - int n = strlen(arg[0]) + 1; - id = new char[n]; - strcpy(id,arg[0]); + id = utils::strdup(arg[0]); igroup = group->find(arg[1]); groupbit = group->bitmask[igroup]; - n = strlen(arg[2]) + 1; - style = new char[n]; - strcpy(style,arg[2]); + style = utils::strdup(arg[2]); - n = strlen(arg[4]) + 1; - filename = new char[n]; - strcpy(filename,arg[4]); + filename = utils::strdup(arg[4]); comm_forward = comm_reverse = 0; @@ -140,9 +134,8 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) filewriter = 1; fileproc = me; MPI_Comm_split(world,me,0,&clustercomm); - multiname = new char[strlen(filename) + 16]; *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,me,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, me, ptr+1)); *ptr = '%'; } @@ -573,14 +566,12 @@ void Dump::openfile() *ptr = '*'; if (maxfiles > 0) { if (numfiles < maxfiles) { - nameslist[numfiles] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[numfiles],filecurrent); + nameslist[numfiles] = utils::strdup(filecurrent); ++numfiles; } else { remove(nameslist[fileidx]); delete[] nameslist[fileidx]; - nameslist[fileidx] = new char[strlen(filecurrent)+1]; - strcpy(nameslist[fileidx],filecurrent); + nameslist[fileidx] = utils::strdup(filecurrent); fileidx = (fileidx + 1) % maxfiles; } } @@ -952,9 +943,7 @@ void Dump::modify_params(int narg, char **arg) int n; if (strstr(arg[iarg+1],"v_") == arg[iarg+1]) { delete [] output->var_dump[idump]; - n = strlen(&arg[iarg+1][2]) + 1; - output->var_dump[idump] = new char[n]; - strcpy(output->var_dump[idump],&arg[iarg+1][2]); + output->var_dump[idump] = utils::strdup(&arg[iarg+1][2]); n = 0; } else { n = utils::inumeric(FLERR,arg[iarg+1],false,lmp); @@ -984,10 +973,9 @@ void Dump::modify_params(int narg, char **arg) MPI_Comm_split(world,icluster,0,&clustercomm); delete [] multiname; - multiname = new char[strlen(filename) + 16]; char *ptr = strchr(filename,'%'); *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,icluster,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, icluster, ptr+1)); *ptr = '%'; iarg += 2; @@ -1028,9 +1016,7 @@ void Dump::modify_params(int narg, char **arg) if (strcmp(arg[iarg+1],"line") == 0) { delete [] format_line_user; - int n = strlen(arg[iarg+2]) + 1; - format_line_user = new char[n]; - strcpy(format_line_user,arg[iarg+2]); + format_line_user = utils::strdup(arg[iarg+2]); iarg += 3; } else { // pass other format options to child classes int n = modify_param(narg-iarg,&arg[iarg]); @@ -1085,10 +1071,9 @@ void Dump::modify_params(int narg, char **arg) MPI_Comm_split(world,icluster,0,&clustercomm); delete [] multiname; - multiname = new char[strlen(filename) + 16]; char *ptr = strchr(filename,'%'); *ptr = '\0'; - sprintf(multiname,"%s%d%s",filename,icluster,ptr+1); + multiname = utils::strdup(fmt::format("{}{}{}", filename, icluster, ptr+1)); *ptr = '%'; iarg += 2; diff --git a/src/dump_cfg.cpp b/src/dump_cfg.cpp index b4e6af90cf..0ff32720d4 100644 --- a/src/dump_cfg.cpp +++ b/src/dump_cfg.cpp @@ -74,14 +74,10 @@ DumpCFG::DumpCFG(LAMMPS *lmp, int narg, char **arg) : |ArgInfo::DNAME|ArgInfo::INAME); if (argi.get_dim() == 1) { - std::string newarg(std::to_string(earg[iarg][0])); - newarg += std::string("_") + argi.get_name(); - newarg += std::string("_") + std::to_string(argi.get_index1()); - auxname[i] = new char[newarg.size()+1]; - strcpy(auxname[i],newarg.c_str()); + std::string newarg = fmt::format("{}_{}_{}", earg[iarg][0], argi.get_name(), argi.get_index1()); + auxname[i] = utils::strdup(newarg); } else { - auxname[i] = new char[strlen(earg[iarg]) + 1]; - strcpy(auxname[i],earg[iarg]); + auxname[i] = utils::strdup(earg[iarg]); } } } diff --git a/src/dump_custom.cpp b/src/dump_custom.cpp index 6f4d7c50b0..53094849ac 100644 --- a/src/dump_custom.cpp +++ b/src/dump_custom.cpp @@ -1562,9 +1562,7 @@ int DumpCustom::add_compute(const char *id) delete [] compute; compute = new Compute*[ncompute+1]; - int n = strlen(id) + 1; - id_compute[ncompute] = new char[n]; - strcpy(id_compute[ncompute],id); + id_compute[ncompute] = utils::strdup(id); ncompute++; return ncompute-1; } @@ -1587,9 +1585,7 @@ int DumpCustom::add_fix(const char *id) delete [] fix; fix = new Fix*[nfix+1]; - int n = strlen(id) + 1; - id_fix[nfix] = new char[n]; - strcpy(id_fix[nfix],id); + id_fix[nfix] = utils::strdup(id); nfix++; return nfix-1; } @@ -1616,9 +1612,7 @@ int DumpCustom::add_variable(const char *id) vbuf = new double*[nvariable+1]; for (int i = 0; i <= nvariable; i++) vbuf[i] = nullptr; - int n = strlen(id) + 1; - id_variable[nvariable] = new char[n]; - strcpy(id_variable[nvariable],id); + id_variable[nvariable] = utils::strdup(id); nvariable++; return nvariable-1; } @@ -1642,9 +1636,7 @@ int DumpCustom::add_custom(const char *id, int flag) flag_custom = (int *) memory->srealloc(flag_custom,(ncustom+1)*sizeof(int),"dump:flag_custom"); - int n = strlen(id) + 1; - id_custom[ncustom] = new char[n]; - strcpy(id_custom[ncustom],id); + id_custom[ncustom] = utils::strdup(id); flag_custom[ncustom] = flag; ncustom++; @@ -1663,9 +1655,7 @@ int DumpCustom::modify_param(int narg, char **arg) if (iregion == -1) error->all(FLERR,"Dump_modify region ID does not exist"); delete [] idregion; - int n = strlen(arg[1]) + 1; - idregion = new char[n]; - strcpy(idregion,arg[1]); + idregion = utils::strdup(arg[1]); } return 2; } @@ -1686,11 +1676,9 @@ int DumpCustom::modify_param(int narg, char **arg) if (strcmp(arg[1],"int") == 0) { delete [] format_int_user; - int n = strlen(arg[2]) + 1; - format_int_user = new char[n]; - strcpy(format_int_user,arg[2]); + format_int_user = utils::strdup(arg[2]); delete [] format_bigint_user; - n = strlen(format_int_user) + 8; + int n = strlen(format_int_user) + 8; format_bigint_user = new char[n]; // replace "d" in format_int_user with bigint format specifier // use of &str[1] removes leading '%' from BIGINT_FORMAT string @@ -1706,18 +1694,14 @@ int DumpCustom::modify_param(int narg, char **arg) } else if (strcmp(arg[1],"float") == 0) { delete [] format_float_user; - int n = strlen(arg[2]) + 1; - format_float_user = new char[n]; - strcpy(format_float_user,arg[2]); + format_float_user = utils::strdup(arg[2]); } else { int i = utils::inumeric(FLERR,arg[1],false,lmp) - 1; if (i < 0 || i >= nfield) error->all(FLERR,"Illegal dump_modify command"); if (format_column_user[i]) delete [] format_column_user[i]; - int n = strlen(arg[2]) + 1; - format_column_user[i] = new char[n]; - strcpy(format_column_user[i],arg[2]); + format_column_user[i] = utils::strdup(arg[2]); } return 3; } @@ -1730,9 +1714,7 @@ int DumpCustom::modify_param(int narg, char **arg) delete [] typenames; typenames = new char*[ntypes+1]; for (int itype = 1; itype <= ntypes; itype++) { - int n = strlen(arg[itype]) + 1; - typenames[itype] = new char[n]; - strcpy(typenames[itype],arg[itype]); + typenames[itype] = utils::strdup(arg[itype]); } return ntypes+1; } @@ -1998,8 +1980,7 @@ int DumpCustom::modify_param(int narg, char **arg) memory->grow(thresh_first,(nthreshlast+1),"dump:thresh_first"); std::string threshid = fmt::format("{}{}_DUMP_STORE",id,nthreshlast); - thresh_fixID[nthreshlast] = new char[threshid.size()+1]; - strcpy(thresh_fixID[nthreshlast],threshid.c_str()); + thresh_fixID[nthreshlast] = utils::strdup(threshid); modify->add_fix(fmt::format("{} {} STORE peratom 1 1",threshid, group->names[igroup])); thresh_fix[nthreshlast] = (FixStore *) modify->fix[modify->nfix-1]; diff --git a/src/dump_local.cpp b/src/dump_local.cpp index beb9236d63..53a82b496f 100644 --- a/src/dump_local.cpp +++ b/src/dump_local.cpp @@ -472,9 +472,7 @@ int DumpLocal::add_compute(const char *id) delete [] compute; compute = new Compute*[ncompute+1]; - int n = strlen(id) + 1; - id_compute[ncompute] = new char[n]; - strcpy(id_compute[ncompute],id); + id_compute[ncompute] = utils::strdup(id); ncompute++; return ncompute-1; } @@ -497,9 +495,7 @@ int DumpLocal::add_fix(const char *id) delete [] fix; fix = new Fix*[nfix+1]; - int n = strlen(id) + 1; - id_fix[nfix] = new char[n]; - strcpy(id_fix[nfix],id); + id_fix[nfix] = utils::strdup(id); nfix++; return nfix-1; } diff --git a/src/dump_xyz.cpp b/src/dump_xyz.cpp index 108ea6f4d6..5f478a2443 100644 --- a/src/dump_xyz.cpp +++ b/src/dump_xyz.cpp @@ -40,10 +40,7 @@ DumpXYZ::DumpXYZ(LAMMPS *lmp, int narg, char **arg) : Dump(lmp, narg, arg), if (format_default) delete [] format_default; - char *str = (char *) "%s %g %g %g"; - int n = strlen(str) + 1; - format_default = new char[n]; - strcpy(format_default,str); + format_default = utils::strdup("%s %g %g %g"); ntypes = atom->ntypes; typenames = nullptr; @@ -71,14 +68,11 @@ void DumpXYZ::init_style() // format = copy of default or user-specified line format delete [] format; - char *str; - if (format_line_user) str = format_line_user; - else str = format_default; - int n = strlen(str) + 2; - format = new char[n]; - strcpy(format,str); - strcat(format,"\n"); + if (format_line_user) + format = utils::strdup(fmt::format("{}\n", format_line_user)); + else + format = utils::strdup(fmt::format("{}\n", format_default)); // initialize typenames array to be backward compatible by default // a 32-bit int can be maximally 10 digits plus sign @@ -119,9 +113,7 @@ int DumpXYZ::modify_param(int narg, char **arg) typenames = new char*[ntypes+1]; for (int itype = 1; itype <= ntypes; itype++) { - int n = strlen(arg[itype]) + 1; - typenames[itype] = new char[n]; - strcpy(typenames[itype],arg[itype]); + typenames[itype] = utils::strdup(arg[itype]); } return ntypes+1;