Hello there,
I have a simple testcode just to see why another program wasn't working.
#include <SimpleList.h>
SimpleList<int *> myList;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int *a;
while(true){
if(Serial.available()){
*a = 'y'; // it was *a = Serial.read(); but it doesn't work with this either
Serial.read(); // just here to wait a little bit
myList.push_back(a);
Serial.println("--Begin *a--");
Serial.println(*a);
Serial.println("--End *a--");
Serial.println("---BEGIN myList---");
for (SimpleList<int *>::iterator itr = myList.begin(); itr != myList.end(); ++itr)
{
Serial.print("Value: ");
Serial.println(**itr);
}
Serial.println("---END myList---");
}
}
}
Then in the serial prompt I recieve something like this:
--Begin *a--
121
--End *a--
---BEGIN myList---
Value: 0
---END myList---
--Begin *a--
121
--End *a--
---BEGIN myList---
Value: 0
Value: 0
---END myList---
I don't quite understand why all the pointers in the list redirect to 0. They all should have the same value as *a.
I made a simple code in C++ to test this.
#include <iostream>
#include <list>
using namespace std;
list<int *> myList;
int main()
{
int *a;
*a = 'h';
while(true){
cin >> *a;
myList.push_back(a);
cout << "--In *a--" << endl;
cout << *a << endl;
cout << "--End *a--" << endl;
cout << "---Begin list---" << endl;
for (list<int *>::iterator itr = myList.begin(); itr != myList.end(); ++itr)
{
cout << "LIST: " << **itr << endl;
}
cout << "---End list---" << endl;
}
return 0;
}
The output is as I thought it should be:
12
--In *a--
12
--End *a--
---Begin list---
LIST: 12
---End list---
14
--In *a--
14
--End *a--
---Begin list---
LIST: 14
LIST: 14
---End list---
So I think it's an issue with SimpleList in Arduino,
What's exactly the problem? How could I make a list of pointers?
Solved: Embarrassing enough I was forgetting to asign memory.