From 2f01f01866a1efe137e0dbd7160165bca44d087e Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Wed, 29 Jun 2016 15:21:10 +0100 Subject: [PATCH] Always allocate at least one element --- include/pybind11/numpy.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 5fdbe6df..43f0d57f 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -127,7 +127,8 @@ public: // allocate zeroed memory if it hasn't been provided auto buf_info = info; if (!buf_info.ptr) - buf_info.ptr = std::calloc(info.size, info.itemsize); + // always allocate at least 1 element, same way as NumPy does it + buf_info.ptr = std::calloc(std::max(info.size, 1ul), info.itemsize); if (!buf_info.ptr) pybind11_fail("NumPy: failed to allocate memory for buffer"); auto view = memoryview(buf_info);