Help with queue

I'm attempting to use this Queue library:

So far I'm not having any luck. Here's my stripped down code, based on this example:

#include <cppQueue.h>

typedef struct strRec {
  char  url[300];
  char  misc1[255];
} Rec;

Rec tab[2] = {
  { "https://whatever1.com", "something1" },
  { "https://whatever2.com", "something2" }
};
cppQueue  queue1(sizeof(Rec), 10, LIFO, true); 


void setup() {
  Serial.begin(115200); 
}

void loop() {
  
    // iterate through the queue
    for (int i = 0 ; i < sizeof(tab)/sizeof(Rec) ; i++)
    {
      Rec rec;
      queue1.pop(&rec);
      Serial.println("There's something in the queue!");
      Serial.println();
      Serial.print(rec.url);
      Serial.print(" ");
      Serial.print(rec.misc1);

      Serial.println();
      String url = rec.url;
      Serial.println("URL = " + url);
      
      Serial.println();
    }   

    delay(1000);
}

However this just prints gibberish to the console:

There's something in the queue! ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮xV⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮xV⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮xV⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮xV⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ ,⸮ URL = ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

Any ideas by chance?

Also, shouldn't calling pop() on the queue remove that item from the queue? Currently it isn't.

There is a separate example for working with strings. Perhaps that helps.

I saw that, and thank you, but I would like to use a struct (of strings).

Ah, I see you did not push() anything, so whatever you pop() is undefined.

You probably want to add something like this (taken from the example):

for (i = 0; i < sizeof(tab) / sizeof(Rec); i++) {
  Rec rec = tab[i];
  q.push(&rec);
}

Thank you! And oops.

My fixed code and simpler example of using the Queue library, on the off chance it's useful to someone:

#include <cppQueue.h>

typedef struct strRec {
  char  url[300];
  char  misc1[255];
} Rec;

cppQueue  queue1(sizeof(Rec), 10, LIFO, true); 

void setup() {
  Serial.begin(115200); 

  Rec test = {"http://google.com", "some stuff"};
  queue1.push(&test);

}

void loop() {  

  // iterate through the queue
  while (!queue1.isEmpty())
  {
    Rec rec;
    queue1.pop(&rec);
    Serial.println("There's something in the queue!");
    Serial.println();
    Serial.print(rec.url);
    Serial.print(" ");
    Serial.print(rec.misc1);

    Serial.println();
    String url = rec.url;
    Serial.println("URL = " + url);
    
    Serial.println();
  }   

  delay(1000);

}

This outputs:

On a sidenote that example that I used as a starting point strikes me as confusing. Why would anyone iterate the queue in that way instead of using "while (!queue1.isEmpty())"? And why does the struct "tab" exist at all?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.