From bdeeaf690b2b2018a0c56f20947040a096bcfe80 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 6 Aug 2015 09:33:37 +0200 Subject: [PATCH] Get rid of memsets --- src/app/app.cpp | 4 ++-- src/common/ioutils.h | 22 ++++++++++++++++++---- src/graphics/engine/particle.cpp | 8 ++++---- src/object/old_object.cpp | 2 +- src/object/old_object_interface.h | 10 +++++----- src/object/task/taskgoto.cpp | 3 +-- src/object/task/taskshield.cpp | 1 - src/script/script.cpp | 3 +-- src/ui/edit.cpp | 2 +- src/ui/screen/screen_level_list.cpp | 8 ++------ 10 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 29f8a4a5..d612c804 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -93,8 +93,8 @@ struct ApplicationPrivate ApplicationPrivate() { - memset(¤tEvent, 0, sizeof(SDL_Event)); - memset(&lastMouseMotionEvent, 0, sizeof(SDL_Event)); + SDL_memset(¤tEvent, 0, sizeof(SDL_Event)); + SDL_memset(&lastMouseMotionEvent, 0, sizeof(SDL_Event)); surface = nullptr; joystick = nullptr; joystickTimer = 0; diff --git a/src/common/ioutils.h b/src/common/ioutils.h index 9e2be67c..676be94b 100644 --- a/src/common/ioutils.h +++ b/src/common/ioutils.h @@ -94,8 +94,15 @@ inline bool ReadBinaryBool(std::istream &istr) */ inline void WriteBinaryFloat(float value, std::ostream &ostr) { - union { float fValue; unsigned int iValue; } u; - memset(&u, 0, sizeof(u)); + union FloatCast + { + float fValue; + unsigned int iValue; + }; + FloatCast u; + u.fValue = 0.0f; + u.iValue = 0; + u.fValue = value; IOUtils::WriteBinary<4, unsigned int>(u.iValue, ostr); } @@ -107,8 +114,15 @@ inline void WriteBinaryFloat(float value, std::ostream &ostr) */ inline float ReadBinaryFloat(std::istream &istr) { - union { float fValue; unsigned int iValue; } u; - memset(&u, 0, sizeof(u)); + union FloatCast + { + float fValue; + unsigned int iValue; + }; + FloatCast u; + u.fValue = 0.0f; + u.iValue = 0; + u.iValue = IOUtils::ReadBinary<4, unsigned int>(istr); return u.fValue; } diff --git a/src/graphics/engine/particle.cpp b/src/graphics/engine/particle.cpp index 200a1610..c379ac88 100644 --- a/src/graphics/engine/particle.cpp +++ b/src/graphics/engine/particle.cpp @@ -317,7 +317,7 @@ int CParticle::CreateParticle(Math::Vector pos, Math::Vector speed, Math::Point if (! m_particle[i].used) { - memset(&m_particle[i], 0, sizeof(Particle)); + m_particle[i] = Particle(); m_particle[i].used = true; m_particle[i].ray = false; m_particle[i].uniqueStamp = m_uniqueStamp++; @@ -387,7 +387,7 @@ int CParticle::CreateFrag(Math::Vector pos, Math::Vector speed, if (!m_particle[i].used) { - memset(&m_particle[i], 0, sizeof(Particle)); + m_particle[i] = Particle(); m_particle[i].used = true; m_particle[i].ray = false; m_particle[i].uniqueStamp = m_uniqueStamp++; @@ -491,7 +491,7 @@ int CParticle::CreatePart(Math::Vector pos, Math::Vector speed, if (!m_particle[i].used) { - memset(&m_particle[i], 0, sizeof(Particle)); + m_particle[i] = Particle(); m_particle[i].used = true; m_particle[i].ray = false; m_particle[i].uniqueStamp = m_uniqueStamp++; @@ -547,7 +547,7 @@ int CParticle::CreateRay(Math::Vector pos, Math::Vector goal, if (!m_particle[i].used) { - memset(&m_particle[i], 0, sizeof(Particle)); + m_particle[i] = Particle(); m_particle[i].used = true; m_particle[i].ray = true; m_particle[i].uniqueStamp = m_uniqueStamp++; diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index b0df3e92..a6f51b3e 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -161,7 +161,7 @@ COldObject::COldObject(int id) m_infoReturn = NAN; m_team = 0; - memset(&m_character, 0, sizeof(m_character)); + m_character = Character(); m_character.wheelFront = 1.0f; m_character.wheelBack = 1.0f; m_character.wheelLeft = 1.0f; diff --git a/src/object/old_object_interface.h b/src/object/old_object_interface.h index d937e2e8..166a262f 100644 --- a/src/object/old_object_interface.h +++ b/src/object/old_object_interface.h @@ -48,11 +48,11 @@ struct Program; struct Character { - float wheelFront; // position X of the front wheels - float wheelBack; // position X of the back wheels - float wheelLeft; // position Z of the left wheels - float wheelRight; // position Z of the right wheels - float height; // normal height on top of ground + float wheelFront = 0.0f; // position X of the front wheels + float wheelBack = 0.0f; // position X of the back wheels + float wheelLeft = 0.0f; // position Z of the left wheels + float wheelRight = 0.0f; // position Z of the right wheels + float height = 0.0f; // normal height on top of ground Math::Vector posPower; // position of the battery }; diff --git a/src/object/task/taskgoto.cpp b/src/object/task/taskgoto.cpp index 057ac4b8..2b2d2e56 100644 --- a/src/object/task/taskgoto.cpp +++ b/src/object/task/taskgoto.cpp @@ -2063,8 +2063,7 @@ bool CTaskGoto::BitmapOpen() BitmapClose(); m_bmSize = static_cast(3200.0f/BM_DIM_STEP); - m_bmArray = new unsigned char[m_bmSize*m_bmSize/8*2]; - memset(m_bmArray, 0, m_bmSize*m_bmSize/8*2); + m_bmArray = new unsigned char[m_bmSize*m_bmSize/8*2](); m_bmOffset = m_bmSize/2; m_bmLine = m_bmSize/8; diff --git a/src/object/task/taskshield.cpp b/src/object/task/taskshield.cpp index 694cfbaf..da06c8fb 100644 --- a/src/object/task/taskshield.cpp +++ b/src/object/task/taskshield.cpp @@ -525,7 +525,6 @@ bool CTaskShield::CreateLight(Math::Vector pos) if ( !m_engine->GetLightMode() ) return true; - memset(&light, 0, sizeof(light)); light.type = Gfx::LIGHT_SPOT; light.ambient = Gfx::Color(0.0f, 0.0f, 0.0f); light.diffuse = Gfx::Color(0.0f, 1.0f, 2.0f); diff --git a/src/script/script.cpp b/src/script/script.cpp index 428a1162..3bd130d8 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -358,8 +358,7 @@ bool CScript::Run() if ( m_bStepMode ) // step by step mode? { - Event newEvent; - memset(&newEvent, 0, sizeof(Event)); + Event newEvent; Step(newEvent); } diff --git a/src/ui/edit.cpp b/src/ui/edit.cpp index a3f1d357..c302b8da 100644 --- a/src/ui/edit.cpp +++ b/src/ui/edit.cpp @@ -101,7 +101,7 @@ CEdit::CEdit () : CControl () m_text = std::vector(m_maxChar+1, '\0'); m_len = 0; - memset(m_lineOffset, 0, sizeof(int) * EDITLINEMAX); + std::fill_n(m_lineOffset, EDITLINEMAX, 0); m_fontType = Gfx::FONT_COURIER; m_scroll = 0; diff --git a/src/ui/screen/screen_level_list.cpp b/src/ui/screen/screen_level_list.cpp index 209e51c0..b1f14201 100644 --- a/src/ui/screen/screen_level_list.cpp +++ b/src/ui/screen/screen_level_list.cpp @@ -321,11 +321,9 @@ void CScreenLevelList::UpdateSceneChap(int &chap) CList* pl; std::string fileName; - char line[500]; + char line[500] = {0}; bool bPassed; - memset(line, 0, 500); - pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); if ( pw == 0 ) return; pl = static_cast(pw->SearchControl(EVENT_INTERFACE_CHAP)); @@ -403,12 +401,10 @@ void CScreenLevelList::UpdateSceneList(int chap, int &sel) CWindow* pw; CList* pl; std::string fileName; - char line[500]; + char line[500] = {0}; int j; bool bPassed; - memset(line, 0, 500); - pw = static_cast(m_interface->SearchControl(EVENT_WINDOW5)); if ( pw == 0 ) return; pl = static_cast(pw->SearchControl(EVENT_INTERFACE_LIST));