user23062: I'm struggling with this issue that I can seem to get around of.
 When defining a vector class, I'm seeming to get some issues when deleting the allocated pointer.
Oddly enough, this only happens after I "read" the memory. What I mean by this is that if I push back a series of values to the vector, they seem to stay there. But once I read the values, the data pointer seems to become invalid of some sort so I crash when trying to deallocate it.
Code:
#ifndef VECTOR_H
#define VECTOR_H

#include <string.h>     /* memcpy */

#define min(a,b) (a < b) ? a : b

template <typename T>
class vector {
public:
    vector() : __capacity(0), __count(0), __data(0) {} //empty constructor
    vector(T obj, int count = 1) {

        if (this->__data) { // free data ptr if used
            delete this->__data;
        }

        this->__data = new T[count];
        this->__count = count;
        this->__capacity = count;

        for (int i = 0; i < count; i++) //populate array with given object
            this->__data[i] = obj;

    }

    ~vector() {

        if (this->__data) { // free data ptr if used
            delete [] this->__data;
        }

        this->__count = this->__capacity = 0;
    }

    T const & operator[] (unsigned int idx) const {
        if (idx < this->__count) {
            return this->__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }

    T& operator[] (unsigned int idx) {
        if (idx < this->__count) {
            return this->__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }

    void resize_to_fit() {
        resize(this->__count);
    }

    T& pop_back(){
        return this->__data[--this->__count];
    }

    void push_back(T obj) {

        if (this->__capacity == this->__count) {
            resize(this->__capacity + 1);
        }

        this->__data[this->__count++] = obj;
    }

    bool isempty() {
        return !this->__data ||
            !this->capacity ||
            !this->size;
    }

    void clear() {
        this->~vector();
    }

    T* data() {
        return this->__data;
    }

    int size() {
        return this->__count;
    }

    int capacity() {
        return this->__capacity;
    }
private:
    void resize(int capacity) {
        if (this->__data == nullptr) {
            this->__data = new T[capacity];

            this->__count = 0;
            this->__capacity = capacity;
        }
        else if (capacity != this->__capacity) { //else do nothing

            T* data = new T[capacity];

            this->__count = min(this->__count, capacity);
            this->__capacity = capacity;

            memcpy(data, this->__data, sizeof(T) * this->__count);

            delete this->__data; //program crashes here, but the pointer is already broken...
            this->__data = data;
        }
    }
protected:

        int __capacity;
        int __count;
        T* __data;



};
#endif//VECTOR_H

I was using this code in my Arduino, not sure if it helps 
void print_vec(vector<int> v){
  Serial.println("Printing new vec!");
  for( int i = 0; i < v.size(); i++){
    Serial.println(v[i]);
  }
}

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); // Open serial connection to report values to host

  while(!Serial); //Waiting for serial to open

  vector<int> i = vector<int>();

  i.push_back(10);
  i.push_back(2);
  print_vec(i); //prints 10 and 2, perfect so far
  i.push_back(3);

  while(true){
    print_vec(i); 
    i.pop_back();
    delay(2000);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

This code outputs:
Printing new vec!
10
2
Printing new vec!
0
2
3
Printing new vec!
0
2
Printing new vec!
0
(...)
What could be causing this? I'm stonewalled for a while now, any insight on how to solve this is appreciated. Thanks!

user10794: You had a lot of problems with your code so it's hard to know where to start. :)
I agree with what John Burger said.
I took your code onto my PC to save mucking around uploading it each time, and also so I could use valgrind on it. Certainly valgrind reported an error after you printed the vector. The reason for that is simple. You passed a copy of the class to print_vec which meant that the destructor got called when print_vec exited, thus deallocating the memory. You should have had a copy constructor. Without it the compiler makes a bitwise copy of the class, which means you now have two objects sharing the same allocated memory.
A quick and dirty fix is to call print_vec by reference:
void print_vec(vector<int> & v){

However that leaves the bug lurking around for the future.
I implemented the copy constructor in my example below, however calling print_vec by reference saves the class having to be copied, reducing the number of new/deletes you are doing, thereby possibly reducing memory fragmentation.

As John Burger said: do not call the destructor yourself! You can't do that. If you want to do the same thing in the destructor and the clear function, just get the destructor to call clear().

The use of leading double-underscores in variables is contrary to the C++ Standard. Don't do that. Use a trailing underscore if you want to indicate a member variable. Lose all the this-> references. They just clutter things up.

Since you allocate an array you should use delete [] - you did that in one place but not the other.

Why do this? It's an unnecessary assignment:
  vector<int> i = vector<int>();

Just do:
  vector<int> i;

If you are going to assign the class for some reason you should also implement an operator= or you will have the same problems as you had with the copy constructor. (See my example).

Here you are testing functions (capacity and size) without calling them:
bool isempty() {
    return !this->__data ||
        !this->capacity ||
        !this->size;
}


You can't really do this:
  T const & operator[] (unsigned int idx) const {
        if (idx < this->__count) {
            return this->__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }

If you take the "else" path then the function is returning no value (you get a compiler warning if you turn warnings on).

My reworked example, with my suggestions in it. It compiles without warnings or errors and runs without crashing (on a PC):
//      g++ -std=c++11 -g3 test.cpp -o test
// OR:  clang -std=c++11 -g3 -Wall test.cpp -lstdc++ -o test
//
// valgrind --leak-check=yes ./test

#include <stdio.h>      // printf
#include <string.h>     /* memcpy */

#define min(a,b) (a < b) ? a : b

template <typename T>
class vector {
public:
    //empty constructor
    vector() : capacity_(0), count_(0), data_(nullptr) {}

    // copy constructor
    vector (const vector & rhs) : capacity_(0), count_(0), data_(nullptr)
      {
      data_ = new T [rhs.capacity_];
      capacity_ = rhs.capacity_;
      count_ = rhs.count_;
      memcpy (data_, rhs.data_, sizeof (T) * count_);
      }  // end of copy constructor

    // operator=
    vector & operator= (const vector & rhs)
      {
      // gracefully handle self-assignment (eg. a = a;)
      if (this == &rhs )
        return *this;

      data_ = new T [rhs.capacity_];
      capacity_ = rhs.capacity_;
      count_ = rhs.count_;
      memcpy (data_, rhs.data_, sizeof (T) * count_);
      return *this;
      }  // end of operator=

    // destructor
    ~vector()
      {
        clear ();
      }  // end of destructor

    T const & operator[] (unsigned int idx) const {
       return data_[idx];
    }

    T& operator[] (unsigned int idx) {
       return data_[idx];
    }

    void resize_to_fit() {
        resize(count_);
    }

    T& pop_back(){
        return data_[--count_];
    }

    void push_back(T obj) 
        {
        if (capacity_ == count_) {
            resize(capacity_ + 1);
        }

        data_[count_++] = obj;
    }

    bool isempty() {
        return count_ == 0;
    }

    void clear() {
      delete [] data_;
      data_ = nullptr;
      count_ = capacity_ = 0;
    }

    T* data() {
        return data_;
    }

    int size() {
        return count_;
    }

    int capacity() {
        return capacity_;
    }
private:
    void resize(int capacity) {
        if (data_ == nullptr) {
            data_ = new T[capacity];
            count_ = 0;
            capacity_ = capacity;
        }
        else if (capacity > capacity_) { //else do nothing

            // allocate new memory
            T* data = new T[capacity];
            // count can't be higher than capacity
            count_ = min(count_, capacity);
            capacity_ = capacity;
            // copy data across to new pointer
            memcpy(data, data_, sizeof(T) * count_);
            // delete old pointer
            delete [] data_;
            // now remember the new pointer
            data_ = data;
        }
    }
protected:

        int capacity_;
        int count_;
        T* data_;
};



void print_vec(vector<int> v){
  printf("%s\n", "Printing new vec!");
  for( int i = 0; i < v.size(); i++){
    printf("%i\n", v[i]);
  }
}

int main() {

  vector<int> i;

  i.push_back(10);
  i.push_back(2);
  print_vec(i);
  i.push_back(3);

  vector<int> j;

  // test assignment
  j = i;
  print_vec(j);
  print_vec(i);


  while(i.size () > 0){
    print_vec(i);
    i.pop_back();
  }


}


user22924: In your second constructor you start with the following code:
if (this->__data) { // free data ptr if used
    delete this->__data;
} // if

This is deadly! You've not initialised __data, so it could hold any value under the Sun. And there is no way that it could possibly be a valid pointer to existing data - it's a brand-new uninitialized object. Returning this garbage pointer to the heap is simply asking for trouble.
Remove those lines - or even better, use an initialiser list like you did with the first constructor:
vector(T obj, int count = 1) :
       __data(new T[count]),
       __count(count),
       __capacity(count)
{
    for (int i = 0; i < count; i++) //populate array with given object
        __data[i] = obj;
} // vector(T, count)

Another problem: In your clear() member you wrote:
void clear() {
    this->~vector();
}

You should never, never, never directly call a destructor like this. The compiler can put all sorts of other code in the destructor code, since it "knows" that it's the only one that will ever call it. For example, some compilers pre-push a flag to say whether to delete the object from the heap after doing the destruction. You haven't done that, so the above can corrupt all sorts of things.
Instead, move the code that is in the destructor into clear(), and then simply call clear() from the destructor.
Another one, this one picked up by @Nick Gammon:
bool isempty() {
    return !this->__data ||
           !this->capacity ||
           !this->size;
}

This is testing whether the __data member is false, and whether the capacity and size functions have been defined. Don't worry: the latter two have been... you missed the prefix __...

[Also, stop it with the this-> everywhere. You've used a __ before all your member variables (which is itself against convention); you don't need to hammer home the fact that they are member variables: the compiler already knows.]


23062: <p>I'm struggling with this issue that I can seem to get around of.
 When defining a vector class, I'm seeming to get some issues when deleting the allocated pointer.</p>

<p>Oddly enough, this only happens after I "read" the memory. What I mean by this is that if I push back a series of values to the vector, they seem to stay there. But once I read the values, the data pointer seems to become invalid of some sort so I crash when trying to deallocate it.</p>

<p>Code:</p>

<pre><code>#ifndef VECTOR_H
#define VECTOR_H

#include &lt;string.h&gt;     /* memcpy */

#define min(a,b) (a &lt; b) ? a : b

template &lt;typename T&gt;
class vector {
public:
    vector() : __capacity(0), __count(0), __data(0) {} //empty constructor
    vector(T obj, int count = 1) {

        if (this-&gt;__data) { // free data ptr if used
            delete this-&gt;__data;
        }

        this-&gt;__data = new T[count];
        this-&gt;__count = count;
        this-&gt;__capacity = count;

        for (int i = 0; i &lt; count; i++) //populate array with given object
            this-&gt;__data[i] = obj;

    }

    ~vector() {

        if (this-&gt;__data) { // free data ptr if used
            delete [] this-&gt;__data;
        }

        this-&gt;__count = this-&gt;__capacity = 0;
    }

    T const &amp; operator[] (unsigned int idx) const {
        if (idx &lt; this-&gt;__count) {
            return this-&gt;__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }

    T&amp; operator[] (unsigned int idx) {
        if (idx &lt; this-&gt;__count) {
            return this-&gt;__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }

    void resize_to_fit() {
        resize(this-&gt;__count);
    }

    T&amp; pop_back(){
        return this-&gt;__data[--this-&gt;__count];
    }

    void push_back(T obj) {

        if (this-&gt;__capacity == this-&gt;__count) {
            resize(this-&gt;__capacity + 1);
        }

        this-&gt;__data[this-&gt;__count++] = obj;
    }

    bool isempty() {
        return !this-&gt;__data ||
            !this-&gt;capacity ||
            !this-&gt;size;
    }

    void clear() {
        this-&gt;~vector();
    }

    T* data() {
        return this-&gt;__data;
    }

    int size() {
        return this-&gt;__count;
    }

    int capacity() {
        return this-&gt;__capacity;
    }
private:
    void resize(int capacity) {
        if (this-&gt;__data == nullptr) {
            this-&gt;__data = new T[capacity];

            this-&gt;__count = 0;
            this-&gt;__capacity = capacity;
        }
        else if (capacity != this-&gt;__capacity) { //else do nothing

            T* data = new T[capacity];

            this-&gt;__count = min(this-&gt;__count, capacity);
            this-&gt;__capacity = capacity;

            memcpy(data, this-&gt;__data, sizeof(T) * this-&gt;__count);

            delete this-&gt;__data; //program crashes here, but the pointer is already broken...
            this-&gt;__data = data;
        }
    }
protected:

        int __capacity;
        int __count;
        T* __data;



};
#endif//VECTOR_H
</code></pre>

<p>I was using this code in my Arduino, not sure if it helps </p>

<pre><code>void print_vec(vector&lt;int&gt; v){
  Serial.println("Printing new vec!");
  for( int i = 0; i &lt; v.size(); i++){
    Serial.println(v[i]);
  }
}

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); // Open serial connection to report values to host

  while(!Serial); //Waiting for serial to open

  vector&lt;int&gt; i = vector&lt;int&gt;();

  i.push_back(10);
  i.push_back(2);
  print_vec(i); //prints 10 and 2, perfect so far
  i.push_back(3);

  while(true){
    print_vec(i); 
    i.pop_back();
    delay(2000);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
</code></pre>

<p>This code outputs:
Printing new vec!
10
2
Printing new vec!
0
2
3
Printing new vec!
0
2
Printing new vec!
0
(...)</p>

<p>What could be causing this? I'm stonewalled for a while now, any insight on how to solve this is appreciated. Thanks!</p>
 10794: <p>You had a lot of problems with your code so it's hard to know where to start. :)</p>

<p>I agree with what John Burger said.</p>

<p>I took your code onto my PC to save mucking around uploading it each time, and also so I could use valgrind on it. Certainly valgrind reported an error after you printed the vector. The reason for that is simple. You passed a <strong>copy</strong> of the class to <code>print_vec</code> which meant that the destructor got called when <code>print_vec</code> exited, thus deallocating the memory. You should have had a <em>copy constructor</em>. Without it the compiler makes a bitwise copy of the class, which means you now have two objects sharing the same allocated memory.</p>

<p>A quick and dirty fix is to call <code>print_vec</code> by reference:</p>

<pre><code>void print_vec(vector&lt;int&gt; &amp; v){
</code></pre>

<p>However that leaves the bug lurking around for the future.</p>

<p>I implemented the copy constructor in my example below, however calling <code>print_vec</code> by reference saves the class having to be copied, reducing the number of <code>new/delete</code>s you are doing, thereby possibly reducing memory fragmentation.</p>

<hr>

<p>As John Burger said: <strong>do not call the destructor yourself!</strong> You can't do that. If you want to do the same thing in the destructor and the <code>clear</code> function, just get the destructor to call <code>clear()</code>.</p>

<hr>

<p>The use of leading double-underscores in variables is contrary to the C++ Standard. Don't do that. Use a trailing underscore if you want to indicate a member variable. Lose all the <code>this-&gt;</code> references. They just clutter things up.</p>

<hr>

<p>Since you allocate an array you should use <code>delete []</code> - you did that in one place but not the other.</p>

<hr>

<p>Why do this? It's an unnecessary assignment:</p>

<pre><code>  vector&lt;int&gt; i = vector&lt;int&gt;();
</code></pre>

<p>Just do:</p>

<pre><code>  vector&lt;int&gt; i;
</code></pre>

<p>If you are going to assign the class for some reason you should also implement an <code>operator=</code> or you will have the same problems as you had with the copy constructor. (See my example).</p>

<hr>

<p>Here you are testing functions (capacity and size) without calling them:</p>

<pre><code>bool isempty() {
    return !this-&gt;__data ||
        !this-&gt;capacity ||
        !this-&gt;size;
}
</code></pre>

<hr>

<p>You can't really do this:</p>

<pre><code>  T const &amp; operator[] (unsigned int idx) const {
        if (idx &lt; this-&gt;__count) {
            return this-&gt;__data[idx];
        }
        else {
            // throw exception or handle error to be implemented
        }
    }
</code></pre>

<p>If you take the "else" path then the function is returning no value (you get a compiler warning if you turn warnings on).</p>

<hr>

<p>My reworked example, with my suggestions in it. It compiles without warnings or errors and runs without crashing (on a PC):</p>

<pre><code>//      g++ -std=c++11 -g3 test.cpp -o test
// OR:  clang -std=c++11 -g3 -Wall test.cpp -lstdc++ -o test
//
// valgrind --leak-check=yes ./test

#include &lt;stdio.h&gt;      // printf
#include &lt;string.h&gt;     /* memcpy */

#define min(a,b) (a &lt; b) ? a : b

template &lt;typename T&gt;
class vector {
public:
    //empty constructor
    vector() : capacity_(0), count_(0), data_(nullptr) {}

    // copy constructor
    vector (const vector &amp; rhs) : capacity_(0), count_(0), data_(nullptr)
      {
      data_ = new T [rhs.capacity_];
      capacity_ = rhs.capacity_;
      count_ = rhs.count_;
      memcpy (data_, rhs.data_, sizeof (T) * count_);
      }  // end of copy constructor

    // operator=
    vector &amp; operator= (const vector &amp; rhs)
      {
      // gracefully handle self-assignment (eg. a = a;)
      if (this == &amp;rhs )
        return *this;

      data_ = new T [rhs.capacity_];
      capacity_ = rhs.capacity_;
      count_ = rhs.count_;
      memcpy (data_, rhs.data_, sizeof (T) * count_);
      return *this;
      }  // end of operator=

    // destructor
    ~vector()
      {
        clear ();
      }  // end of destructor

    T const &amp; operator[] (unsigned int idx) const {
       return data_[idx];
    }

    T&amp; operator[] (unsigned int idx) {
       return data_[idx];
    }

    void resize_to_fit() {
        resize(count_);
    }

    T&amp; pop_back(){
        return data_[--count_];
    }

    void push_back(T obj) 
        {
        if (capacity_ == count_) {
            resize(capacity_ + 1);
        }

        data_[count_++] = obj;
    }

    bool isempty() {
        return count_ == 0;
    }

    void clear() {
      delete [] data_;
      data_ = nullptr;
      count_ = capacity_ = 0;
    }

    T* data() {
        return data_;
    }

    int size() {
        return count_;
    }

    int capacity() {
        return capacity_;
    }
private:
    void resize(int capacity) {
        if (data_ == nullptr) {
            data_ = new T[capacity];
            count_ = 0;
            capacity_ = capacity;
        }
        else if (capacity &gt; capacity_) { //else do nothing

            // allocate new memory
            T* data = new T[capacity];
            // count can't be higher than capacity
            count_ = min(count_, capacity);
            capacity_ = capacity;
            // copy data across to new pointer
            memcpy(data, data_, sizeof(T) * count_);
            // delete old pointer
            delete [] data_;
            // now remember the new pointer
            data_ = data;
        }
    }
protected:

        int capacity_;
        int count_;
        T* data_;
};



void print_vec(vector&lt;int&gt; v){
  printf("%s\n", "Printing new vec!");
  for( int i = 0; i &lt; v.size(); i++){
    printf("%i\n", v[i]);
  }
}

int main() {

  vector&lt;int&gt; i;

  i.push_back(10);
  i.push_back(2);
  print_vec(i);
  i.push_back(3);

  vector&lt;int&gt; j;

  // test assignment
  j = i;
  print_vec(j);
  print_vec(i);


  while(i.size () &gt; 0){
    print_vec(i);
    i.pop_back();
  }


}
</code></pre>
 22924: <h3>In your second constructor you start with the following code:</h3>

<pre><code>if (this-&gt;__data) { // free data ptr if used
    delete this-&gt;__data;
} // if
</code></pre>

<p>This is deadly! You've not initialised <code>__data</code>, so it could hold <em>any</em> value under the Sun. And there is <em>no way</em> that it could <em>possibly</em> be a valid pointer to existing data - it's a brand-new uninitialized object. Returning this garbage pointer to the heap is simply asking for trouble.</p>

<p>Remove those lines - or even better, use an initialiser list like you did with the first constructor:</p>

<pre><code>vector(T obj, int count = 1) :
       __data(new T[count]),
       __count(count),
       __capacity(count)
{
    for (int i = 0; i &lt; count; i++) //populate array with given object
        __data[i] = obj;
} // vector(T, count)
</code></pre>

<h3>Another problem: In your <code>clear()</code> member you wrote:</h3>

<pre><code>void clear() {
    this-&gt;~vector();
}
</code></pre>

<p>You should never, <em>never</em>, <strong>never</strong> directly call a destructor like this. The compiler can put all sorts of other code in the destructor code, since it "knows" that it's the only one that will ever call it. For example, some compilers pre-push a flag to say whether to <code>delete</code> the object from the heap after doing the destruction. You haven't done that, so the above can corrupt all sorts of things.</p>

<p>Instead, move the code that is in the destructor into <code>clear()</code>, and then simply call <code>clear()</code> from the destructor.</p>

<h3>Another one, this one picked up by @Nick Gammon:</h3>

<pre><code>bool isempty() {
    return !this-&gt;__data ||
           !this-&gt;capacity ||
           !this-&gt;size;
}
</code></pre>

<p>This is testing whether the <code>__data</code> member is false, and whether the <code>capacity</code> and <code>size</code> functions have been defined. Don't worry: the latter two have been... you missed the prefix <code>__</code>...</p>

<blockquote>
  <p>[Also, stop it with the <code>this-&gt;</code> everywhere. You've used a <code>__</code> before all your member variables (which is itself against convention); you don't need to hammer home the fact that they are member variables: the compiler already knows.]</p>
</blockquote>
