Skip to content

Commit

Permalink
The memory error fixed
Browse files Browse the repository at this point in the history
-operator added
  • Loading branch information
roma160 committed Mar 21, 2021
1 parent 4c805c0 commit 1c0d6fd
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 47 deletions.
26 changes: 26 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "Release",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ]
}
]
}
17 changes: 8 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
using namespace std;

int main(){
//std::initializer_list<int> initializerList{};
unsigned long arr[1000];
for(unsigned long & i : arr)
i = 1;
vec<unsigned long> vector(1000, arr);
std::cout<<vector<<"\n";
vec<unsigned long> t(0);
t = vector + vector;
cout<<t;
vec<int> a({ 1, 2, 3 }), b({ 4, 5, 6, 7 });
vec<int> c(0);
c = a + b;
a += b;
c -= a;
cout << "a : " << a << "\n";
cout << "b : " << b << "\n";
cout << "c : " << c << "\n";
return 0;
}
12 changes: 9 additions & 3 deletions src/math/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#include "stringable.h"
#include <initializer_list>
#include <exception>
#include <stdexcept>

template <typename T>
class vec : public stringable {
private:
T* vec_data;
int vec_size;

public:
vec();
vec(int size);
Expand All @@ -30,11 +30,17 @@ class vec : public stringable {
//Operators override
T& operator[](int i);
vec& operator=(vec b);
vec operator+(T b);
vec operator+(vec b);

vec operator+(T b) const;
vec operator+(vec b) const;
void operator+=(T b);
void operator+=(vec b);

vec operator-(T b) const;
vec operator-(vec b) const;
void operator-=(T b);
void operator-=(vec b);

std::string to_string() const override;
};

Expand Down
150 changes: 115 additions & 35 deletions src/math/vec.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
#include <iostream>
#include <sstream>
#include <cstdio>
#include <exception>
#include "vec.h"

#ifdef _DEBUG
#define DEBUG_MSG(msg) std::cout<<msg<<"\n"
#else
#define DEBUG_MSG(msg)
#endif

// Constructors :
template<typename T>
vec<T>::vec(int size) {
vec_data = new T[size];
Expand All @@ -22,7 +30,7 @@ vec<T>::vec(int size, T *values) : vec(size) {
}
template<typename T>
vec<T>::vec(const vec &to_copy) : vec(to_copy.vec_size) {
//std::cout<<to_copy.vec_data<<"\n";
DEBUG_MSG("VEC.tpp : copy constructor call");
std::copy(to_copy.vec_data,
to_copy.vec_data + vec_size,
vec_data);
Expand All @@ -31,71 +39,143 @@ template<typename T>
vec<T>::vec() : vec(0) {}
template<typename T>
vec<T>::~vec() {
DEBUG_MSG("VEC.tpp : destructor call");
delete[] vec_data;
}

template<typename T>
T& vec<T>::operator[](int i) {
#ifdef _DEBUG
if(i >= vec_size) {
std::stringstream ss;
ss<<"You\'ve tried to access "<<i<<"th ";
ss<<"element of vec with size of "<<vec_size;
throw std::out_of_range(ss.str());
}
#endif
return vec_data[i];
}
template<typename T>
vec<T>& vec<T>::operator=(const vec b) {
delete[](vec_data);
DEBUG_MSG("The VEC.h assigment operator call");
if (vec_data == b.vec_data)
return *this;

vec_size = b.vec_size;
delete[](vec_data);
vec_data = new int[vec_size];
std::copy(b.vec_data,
b.vec_data + vec_size,
vec_data);
return *this;
}

template<typename T>
std::string vec<T>::to_string() const {
std::stringstream ss;
ss<<"vec["<<vec_size<<"]{ ";
if(vec_size >= 1)
ss<<vec_data[0];
for(int i = 1; i < vec_size; i++)
ss<<", "<<vec_data[i];
ss<<" };";
return ss.str();
}

template<typename T>
void vec<T>::operator+=(T b) {
for(int i = 0; i < vec_size; i++)
vec_data[i] += b;
}
template<typename T>
vec<T> vec<T>::operator+(T b) {
vec<T> ret(*this);
for(int i = 0; i < vec_size; i++)
template <typename T>
vec<T> vec<T>::operator+(T b) const
{
vec ret(*this);
for (int i = 0; i < vec_size; i++)
ret.vec_data[i] += b;
return ret;
}

template<typename T>
vec<T> vec<T>::operator+(vec b) {
vec *bigger = this, *smaller = &b;
if(bigger->vec_size < smaller->vec_size)
template <typename T>
vec<T> vec<T>::operator+(vec b) const
{
vec *bigger = (vec*) this, *smaller = &b;
if (bigger->vec_size < smaller->vec_size)
std::swap(bigger, smaller);
vec ret(*bigger);
for(int i = 0; i < smaller->vec_size; i++)
for (int i = 0; i < smaller->vec_size; i++)
ret.vec_data[i] += smaller->vec_data[i];
return ret;
}
template<typename T>
void vec<T>::operator+=(vec b) {
vec *bigger = this, *smaller = &b;
if(bigger->vec_size < smaller->vec_size)

template <typename T>
void vec<T>::operator+=(T b)
{
for (int i = 0; i < vec_size; i++)
vec_data[i] += b;
}

template <typename T>
void vec<T>::operator+=(vec b)
{
vec *bigger = this, *smaller = &b;
if (bigger->vec_size < smaller->vec_size)
std::swap(bigger, smaller);
for(int i = 0; i < smaller->vec_size; i++)
for (int i = 0; i < smaller->vec_size; i++)
bigger->vec_data[i] += smaller->vec_data[i];
if(bigger != this)
*this = *bigger;
*this = *bigger;
}

template <typename T>
vec<T> vec<T>::operator-(T b) const
{
vec ret(*this);
for (int i = 0; i < vec_size; i++)
ret.vec_data[i] -= b;
return ret;
}

template <typename T>
vec<T> vec<T>::operator-(vec b) const
{
vec *bigger = this, *smaller = &b;
bool was_swapped = false;
if (bigger->vec_size < smaller->vec_size) {
std::swap(bigger, smaller);
was_swapped = true;
}
vec ret(bigger->vec_size);
if(!was_swapped)
for (int i = 0; i < smaller->vec_size; i++)
ret.vec_data[i] -= smaller->vec_data[i];
else {
for (int i = 0; i < smaller->vec_size; i++)
ret.vec_data[i] = smaller->vec_data[i] - bigger->vec_data[i];
for (int i = smaller->vec_size; i < bigger->vec_size; i++)
ret.vec_data[i] = -bigger->vec_data[i];
}
return ret;
}

template <typename T>
void vec<T>::operator-=(T b)
{
for (int i = 0; i < vec_size; i++)
vec_data[i] -= b;
}

template <typename T>
void vec<T>::operator-=(vec b)
{
vec* bigger = (vec*) this, * smaller = &b;
bool was_swapped = false;
if (bigger->vec_size < smaller->vec_size) {
std::swap(bigger, smaller);
was_swapped = true;
}
if (!was_swapped)
for (int i = 0; i < smaller->vec_size; i++)
bigger->vec_data[i] -= smaller->vec_data[i];
else {
for (int i = 0; i < smaller->vec_size; i++)
bigger->vec_data[i] = smaller->vec_data[i] - bigger->vec_data[i];
for (int i = smaller->vec_size; i < bigger->vec_size; i++)
bigger->vec_data[i] = -bigger->vec_data[i];
}
*this = *bigger;
}

template<typename T>
std::string vec<T>::to_string() const {
std::stringstream ss;
ss<<"vec["<<vec_size<<"]{ ";
if(vec_size >= 1)
ss<<vec_data[0];
for(int i = 1; i < vec_size; i++)
ss<<", "<<vec_data[i];
ss<<" };";
return ss.str();
}

0 comments on commit 1c0d6fd

Please sign in to comment.