From 06fffedc4f91fe2dd3a583a8032e867d6ad2d5bf Mon Sep 17 00:00:00 2001 From: Roland Kaufmann Date: Fri, 22 Nov 2013 09:38:06 +0100 Subject: [PATCH] Revert "Data copying takes an iterator" The original rationale for commit c39d367 was to pass a pointer since this was all we got from one of the APIs. However, just after the code was changed to copy the data anyway since it would have to be converted from double to float. Reverting actually serves a purpose; as @bska pointed out, taking &data[0] of an empty vector is undefined, whereas it now checks the size and assert here (and not in the standard library) on that condition. --- opm/core/io/eclipse/EclipseWriter.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/opm/core/io/eclipse/EclipseWriter.cpp b/opm/core/io/eclipse/EclipseWriter.cpp index b2d5dd95..81ddd128 100644 --- a/opm/core/io/eclipse/EclipseWriter.cpp +++ b/opm/core/io/eclipse/EclipseWriter.cpp @@ -115,7 +115,7 @@ struct EclipseKeyword : public EclipseHandle { : EclipseHandle ( ecl_kw_alloc (name.c_str(), data.size (), type ()), ecl_kw_free) { - copyData (&data[0], data.size (), offset, stride); + copyData (data, offset, stride); } /// Special initialization from double-precision array which @@ -141,7 +141,7 @@ struct EclipseKeyword : public EclipseHandle { type ()), ecl_kw_free) { const std::vector & data = parser.getValue (name); - copyData (&data[0], data.size (), 0, 1); + copyData (data, 0, 1); } /// Constructor for optional fields @@ -167,10 +167,12 @@ private: /// Helper function that is the meat of the constructor template - void copyData (const U* data, - const int num, + void copyData (std::vector data, const int offset, const int stride) { + // number of elements we can possibly take from the vector + const int num = data.size (); + // range cannot start outside of data set assert(offset >= 0 && offset < num); @@ -205,7 +207,7 @@ EclipseKeyword ::EclipseKeyword ( type ()), ecl_kw_free) { const std::vector & data = parser.getValue (name); - copyData (&data[0], data.size (), 0, 1); + copyData (data, 0, 1); } /// Provide only the float version, since that is the one for which @@ -220,7 +222,7 @@ EclipseKeyword ::EclipseKeyword ( : EclipseHandle ( ecl_kw_alloc (name.c_str(), data.size (), type ()), ecl_kw_free) { - copyData (&data[0], data.size (), offset, stride); + copyData (data, offset, stride); } /**