Home:ALL Converter>Making a linked list in PROGMEM (Arduino)

Making a linked list in PROGMEM (Arduino)

Ask Time:2013-11-28T11:54:18         Author:Tobin

Json Formatter

I'm working on a problem that I think will be nicely solved by basically a (multiply) linked list. However, my platform is an Arduino with very limited SRAM, and so I'd like to keep it all in PROGMEM (using the avr/pgmspace.h library).

I'm having trouble referencing fields of a struct to which I have a pointer. Or, to put it another way, I'm having trouble following my linked list.

Here's some code (i've tried to make it short):

#include <avr/pgmspace.h>

typedef struct list_item
{ 
   const prog_char * header;
   const struct list_item *next_item;
}; 

// declarations 
extern const list_item PROGMEM first_item; 
extern const list_item PROGMEM second_item; 
extern const list_item PROGMEM third_item; 

// name
const prog_char first_header[] PROGMEM = "Foo";
const prog_char second_header[] PROGMEM = "Bar";
const prog_char third_header[] PROGMEM = "Baz";

// instantiation & initialization 
const list_item first_item = { &first_header[0], &second_item }; 
const list_item second_item = { &second_header[0], &third_item }; 
const list_item third_item = { &second_header[0], &first_item }; 

// pointers to our items, just for testing
list_item const * const pointer_to_first_item = &first_item;
list_item const * const pointer_to_second_item = &second_item;
list_item const * const pointer_to_third_item = &third_item;

// prints the address of the pointer passed to it
void print_pointer_address( char * description, const void * pointer)
{
    Serial.print(description);
    Serial.println((unsigned int) pointer,HEX);
}

// a test
void setup()
{
    Serial.begin(57600);

    Serial.println("\n--addresses of everything--");

    print_pointer_address("pointer to first_item = ", pointer_to_first_item);
    print_pointer_address("pointer to second_item = ", pointer_to_second_item);
    print_pointer_address("pointer to third_item = ", pointer_to_third_item);

    Serial.println("\n--go through list via pointers--");

    list_item const * the_next_item;
    the_next_item = pointer_to_first_item;
    print_pointer_address("item 1 = ", the_next_item);

    the_next_item = the_next_item->next_item;
    print_pointer_address("item 2 = ", the_next_item);

    the_next_item = the_next_item->next_item;
    print_pointer_address("item 3 = ", the_next_item);

    the_next_item = the_next_item->next_item;
    print_pointer_address("item 4 = ", the_next_item);

}

void loop()
{
}

It outputs:

--addresses of everything--
pointer to first_item = 68
pointer to second_item = 6C
pointer to third_item = 70

--go through list via pointers--
item 1 = 68
item 2 = 6C
item 3 = 1
item 4 = 5350

My question is: Why does item 3 not equal "70"?

I think possibly, I should be using one of the pgmspace functions to read the structs, but then I don't understand why it seems to work for the first item. Thank you for any help or pointers to something I should read to understand this.

Author:Tobin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20257394/making-a-linked-list-in-progmem-arduino
yy