List of pointers doesn't work [SOLVED]

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.

int *a;
  while(true){
    if(Serial.available()){
      *a = 'y';

Remind me - what does "a" point to?

To 'y' always.
This code is only for testing purposes.

Nice try.
Have another go.

Could you explain me then, why an analogous code is compiled by g++ and works as expected?

You got lucky?
It happens.

I'm just wanting to do a list of pointers to objects.

It didn't work, so I made that test code to see if a list of pointers to int work. Apparently it doesn't.

I just want a way to put pointers to objects inside a SimpleList.

I'm just looking for a solution with SimpleList, I don't want to make a class for something that is already done.

If you could give me a solution (and maybe a clear explanation of why this doesn't work) I would be grateful.

Sorry, I'm on a train at the moment posting from my tablet.
Storing stuff to arbitrary locations is always a Bad Thingtm.
Try initialising "a" to point to something

I'm not storing it, I just need it a good way to work with a variable quantity of objects. So having the memory addresses in a list kind of helps me a lot in this situation.

In C++, list of pointers aren't a strange thing; I don't see why they should be in Arduino.

Try initialising "a" to point to something

Already done, doesn't work either.

I'm not storing it

*a = 'y';
What's that then?

AWOL:

*a = 'y';

What's that then?

A test to check if something works, not that I would do that in a real project.

OK, I'll ask again - where is the 'y' being stored?

You were right, a new solved it.

Bad practices never end... Well Thanks AWOL

Offtopic (and not so off): how could I mark this as Solved?

Object *obj[n] = instance of Object;
int *a = 'y'

<--- no bueno. What data type are you using?

You can edit the first post on the thread and edit the title to add "[solved]"