From 04e65d814fe15a17040a701d46ae669cc9dcd3d8 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Sun, 6 Jul 2014 18:19:34 +0800 Subject: [PATCH] Slightly less memory errors Using more std::vector and less memcpy/memset etc. Still seems to have issues but not segfaulting as much. Fixed m_sign missing in = operator --- src/arbint.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/arbint.cpp b/src/arbint.cpp index c24eb8b..6ea4636 100644 --- a/src/arbint.cpp +++ b/src/arbint.cpp @@ -52,20 +52,14 @@ Arbint::Arbint(const vector & digits) : m_digits(digits), m_sign(false) Arbint & Arbint::operator=(const Arbint & cpy) { - memmove(m_digits.data(), cpy.m_digits.data(), - sizeof(digit_t)*min(m_digits.size(), cpy.m_digits.size())); - if (cpy.m_digits.size() > m_digits.size()) - { - unsigned old_size = m_digits.size(); - m_digits.resize(cpy.m_digits.size()); - memset(m_digits.data()+old_size, 0, sizeof(digit_t)*m_digits.size()-old_size); - } + m_digits = cpy.m_digits; + m_sign = cpy.m_sign; return *this; } void Arbint::Zero() { - memset(m_digits.data(), 0, sizeof(digit_t)*m_digits.size()); + m_digits.resize(1, 0L); } unsigned Arbint::Shrink() @@ -325,7 +319,7 @@ Arbint & Arbint::operator<<=(unsigned amount) bool Arbint::GetBit(unsigned i) const { unsigned digit = i/(8*sizeof(digit_t)); - if (digit > m_digits.size()) + if (digit >= m_digits.size()) return false; i = i % (8*sizeof(digit_t)); @@ -337,7 +331,7 @@ bool Arbint::GetBit(unsigned i) const void Arbint::BitClear(unsigned i) { unsigned digit = i/(8*sizeof(digit_t)); - if (digit > m_digits.size()) + if (digit >= m_digits.size()) return; i = i % (8*sizeof(digit_t)); m_digits[digit] &= ~(1L << i); @@ -346,7 +340,7 @@ void Arbint::BitClear(unsigned i) void Arbint::BitSet(unsigned i) { unsigned digit = i/(8*sizeof(digit_t)); - if (digit > m_digits.size()) + if (digit >= m_digits.size()) { m_digits.resize(digit+1, 0L); } -- 2.20.1