From 8abd0e0aca81d169e350c6728cadabf724f389b3 Mon Sep 17 00:00:00 2001 From: Vardan Akopian Date: Tue, 8 Sep 2015 16:20:51 -0700 Subject: [PATCH] avoid unnecessary temporary memory allocations and make it more exception safe --- src/MatlabIO.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/MatlabIO.cpp b/src/MatlabIO.cpp index a38c31d..caa8b5c 100644 --- a/src/MatlabIO.cpp +++ b/src/MatlabIO.cpp @@ -570,15 +570,12 @@ vector MatlabIO::uncompressVariable(uint32_t& data_type, uint32_t& dbytes, readVariableTag(data_type, dbytes, wbytes, buf); // inflate the remainder of the variable, now that we know its size - char *udata_tmp = new char[dbytes]; + vector udata(dbytes); infstream.avail_out = dbytes; - infstream.next_out = (unsigned char *)udata_tmp; + infstream.next_out = reinterpret_cast(&udata[0]); inflate(&infstream, Z_FINISH); inflateEnd(&infstream); - // convert to a vector - vector udata(udata_tmp, udata_tmp+dbytes); - delete [] udata_tmp; return udata; } @@ -661,10 +658,8 @@ MatlabIOContainer MatlabIO::readBlock(void) { // read the binary data block //printf("\nReading binary data block...\n"); fflush(stdout); - char *data_tmp = new char[dbytes]; - fid_.read(data_tmp, sizeof(char)*dbytes); - vector data(data_tmp, data_tmp+dbytes); - delete [] data_tmp; + vector data(dbytes); + fid_.read(&data[0], sizeof(char)*dbytes); // move the seek head position to the next 64-bit boundary // (but only if the data is uncompressed. Saving yet another 8 tiny bytes...)