Commit 51470460 authored by DIAZ Jerome's avatar DIAZ Jerome Committed by GILLES Sebastien

#0 Replace reinterpret_cast used in binary I/O by a safer combination of...

#0 Replace reinterpret_cast used in binary I/O by a safer combination of std::vector and std::memcpy.

#0 Add a header file required by gcc for std::memcpy.
parent f896837b
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <iomanip> #include <iomanip>
#include <cstring> // required by gcc for std::memcpy.
#include "Utilities/Filesystem/File.hpp" #include "Utilities/Filesystem/File.hpp"
#include "Utilities/OutputFormat/OutputFormat.hpp" #include "Utilities/OutputFormat/OutputFormat.hpp"
...@@ -58,7 +59,10 @@ namespace MoReFEM ...@@ -58,7 +59,10 @@ namespace MoReFEM
std::ofstream::binary); std::ofstream::binary);
const auto array = content.GetArray(); const auto array = content.GetArray();
out.write(reinterpret_cast<const char *>(array), Nvalue * sizeof(double)); const auto Nbits = Nvalue * sizeof(double);
std::vector<char> binary_values(Nbits);
std::memcpy(binary_values.data(), array, Nbits);
out.write(binary_values.data(), static_cast<long>(Nbits));
out.close(); out.close();
break; break;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <fstream> #include <fstream>
#include <cmath> #include <cmath>
#include <cstring> // required by gcc for std::memcpy.
#include "Utilities/Exceptions/Exception.hpp" #include "Utilities/Exceptions/Exception.hpp"
#include "Utilities/OutputFormat/ReadBinaryFile.hpp" #include "Utilities/OutputFormat/ReadBinaryFile.hpp"
...@@ -46,12 +47,12 @@ namespace MoReFEM::Advanced ...@@ -46,12 +47,12 @@ namespace MoReFEM::Advanced
in.close(); in.close();
// Buffer contains the entire file. Convert bytes back into doubles. // Buffer contains the entire file. Convert bytes back into doubles.
const double * values = reinterpret_cast<double *> (buffer.data()); const unsigned long Ndouble_values = static_cast<std::size_t>(block_size) / sizeof(double);
std::vector<double> values(Ndouble_values);
std::memcpy(values.data(), buffer.data(), static_cast<std::size_t>(block_size));
const unsigned long Nvalue_binary = static_cast<std::size_t>(block_size) / sizeof(double); for (std::size_t i = 0u; i < Ndouble_values; ++i)
for (std::size_t i = 0u; i < Nvalue_binary; ++i)
{ {
if (std::fabs(values[i]) <= epsilon) if (std::fabs(values[i]) <= epsilon)
ret.push_back(0.); ret.push_back(0.);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment