Specifically the particle Photon, but the following code works locally and on repl.it, but crashes when run on an Arduino:
#include "StringRingBuffer.h"
StringRingBufferNode::StringRingBufferNode(String value) {
this->value = value;
this->next = this;
}
StringRingBuffer::StringRingBuffer(int maxSize) {
this->_maxSize = maxSize;
this->_currentSize = 0;
this->count = 0;
}
void StringRingBuffer::addString(String value) {
if (!this->_firstNode) {
this->_firstNode = new StringRingBufferNode(value);
this->_lastNode = this->_firstNode;
this->_currentSize++;
this->count++;
} else if (this->_currentSize < this->_maxSize) {
StringRingBufferNode *formerFirst = this->_firstNode;
this->_firstNode = new StringRingBufferNode(value);
formerFirst->next = this->_firstNode;
this->_firstNode->next = this->_lastNode;
this->_currentSize++;
this->count++;
} else {
StringRingBufferNode *formerNext = this->_firstNode->next;
this->_firstNode->next = new StringRingBufferNode(value);
this->_firstNode->next->next = formerNext->next;
this->_lastNode = this->_firstNode->next->next;
this->_firstNode = this->_firstNode->next;
}
}
Here is the header file:
#include "application.h"
class StringRingBufferNode {
public:
StringRingBufferNode(String);
String value;
StringRingBufferNode *next;
private:
};
class StringRingBuffer {
public:
StringRingBuffer(int);
int count;
void addString(String);
private:
int _maxSize;
int _currentSize;
StringRingBufferNode *_firstNode;
StringRingBufferNode *_lastNode;
};
It seems that commenting out
formerFirst->next = this->_firstNode;
this->_firstNode->next = this->_lastNode;
the program runs without exception (albeit incorrectly.) I'm rather new to C++ so a beginner's explanation of what I'm doing wrong would be fantastic! I wish I had more details about the exception to give, but it's on the microcontroller, so all I know is that it breaks.
EDIT: Link to the working repl.it: https://repl.it/Gh0a/14
EDIT EDIT: I believe I found the solution/problem. I was explicitly testing for this condition: if (!this->_firstNode), and for some reason this does not resolve the same way in Arduino that it does in the repl.it I'd still love for anyone to shed some light onto why this is, otherwise I'll answer the question myself and accept it in two days.