[cig-commits] r7652 - short/3D/PyLith/trunk/libsrc/meshio

brad at geodynamics.org brad at geodynamics.org
Thu Jul 12 15:09:02 PDT 2007


Author: brad
Date: 2007-07-12 15:09:02 -0700 (Thu, 12 Jul 2007)
New Revision: 7652

Modified:
   short/3D/PyLith/trunk/libsrc/meshio/BinaryIO.cc
Log:
Cleaned up memory allocation when reading string from binary file.

Modified: short/3D/PyLith/trunk/libsrc/meshio/BinaryIO.cc
===================================================================
--- short/3D/PyLith/trunk/libsrc/meshio/BinaryIO.cc	2007-07-12 19:30:13 UTC (rev 7651)
+++ short/3D/PyLith/trunk/libsrc/meshio/BinaryIO.cc	2007-07-12 22:09:02 UTC (rev 7652)
@@ -23,19 +23,21 @@
 pylith::meshio::BinaryIO::readString(std::ifstream& fin,
 				     const int numChars)
 { // readString
-  char* buffer = (numChars > 0) ? new char[numChars+1] : 0;
-  fin.read(buffer, sizeof(char)*numChars);
-  if (numChars > 0)
+  std::string bstring = "";
+  if (numChars > 0) {
+    char* buffer = new char[numChars+1];
+    fin.read(buffer, sizeof(char)*numChars);
     buffer[numChars] = '\0';
 
-  // get string from buffer
-  std::string bufstring(buffer);
-  delete[] buffer; buffer = 0;
+    // get string from buffer
+    std::string bufstring = buffer;
+    delete[] buffer; buffer = 0;
 
-  // remove whitespace
-  const int iLast = bufstring.find_first_of(" ");
-
-  return bufstring.substr(0, iLast);
+    // remove whitespace
+    const int iLast = bufstring.find_first_of(" ");
+    bstring = bufstring.substr(0, iLast);
+  } // if
+  return std::string(bstring);
 } // readString
 
 // ----------------------------------------------------------------------



More information about the cig-commits mailing list