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!
i.pop_back()
calls should empty the vector.delete this->__data; //program crashes here ...
should usedelete[]
right? Also there is no platform code in your class so you could debug on pc where there are much better debugging tools available.