From 77656543441a130276e0a37ba119270edd9156ff Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 2 Sep 2012 17:30:54 +0300 Subject: [PATCH] Memory allocation changes Use std::copy instead of memcpy Call operator= from copy constructor Allocate memory only when needed, not one byte before --- source/vertexformat.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/source/vertexformat.cpp b/source/vertexformat.cpp index 65b276ba..0a26d942 100644 --- a/source/vertexformat.cpp +++ b/source/vertexformat.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -24,11 +24,7 @@ VertexFormat::VertexFormat(VertexComponent c): VertexFormat::VertexFormat(const VertexFormat &f): data(0) { - if(f.data) - { - data = new unsigned char[(f.data[0]&~7)+8]; - memcpy(data, f.data, f.data[0]+1); - } + *this = f; } VertexFormat &VertexFormat::operator=(const VertexFormat &f) @@ -37,7 +33,7 @@ VertexFormat &VertexFormat::operator=(const VertexFormat &f) if(f.data) { data = new unsigned char[(f.data[0]&~7)+8]; - memcpy(data, f.data, f.data[0]+1); + copy(f.data, f.data+f.data[0]+1, data); } else data = 0; @@ -56,10 +52,10 @@ VertexFormat VertexFormat::operator,(VertexComponent c) const if(r.data) { const unsigned char n = ++r.data[0]; - if((n&7)==7) + if((n&7)==0) { - unsigned char *newdt = new unsigned char[n+9]; - memcpy(newdt, r.data, n); + unsigned char *newdt = new unsigned char[n+8]; + copy(r.data, r.data+n, newdt); delete r.data; r.data = newdt; } -- 2.45.2