"table"-datastructure in Flash-Memory

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I just started to implement a simple datastructure to lookup values by a given key-value.
The datastructure doesn't have to be "hight-performance", the only requirement is that the data is in Flash-Memory (PGM).

( To keep it simple I started my first approach in RAM: )

so I startet defining the structure:

/** \brief Table-Entry Data-Structure */
typedef struct{
	uint8_t key;
	uint8_t value;
}table_entry;

/** \brief Table Data-Structure */
typedef struct{
	 uint8_t len;
	 table_entry *tab; 
}table;

then I wrote a function to access the stored values by a key-value:

uint8_t *table_get_value(table *t, uint8_t key){
	uint8_t i=0;
	while( i<(t->len) ){
		if(t->tab[i].key==key) return &(t->tab[i].value);
		i++;
	}
	return NULL;
};

initialized the data:

table_entry entries[5] = {
	{5,1}, 
	{4,2},
	{1,3},
	{2,4},
	{3,5}	
};
table tab = {
	{5, entries}}
};

But all the time I query the pointer to the value of the key-value "1"

table_get_value(&tab, 1);

I get a NULL-Pointer in return. If I examine the tab data-structure with the "watch"-utility of atmel studio I can see that the pointer of "entries" in "tab" points to zero.

So i thought entries is not known at load time, when tab is initialized, but even

table tab = {
	{5, {{5,1},{4,2},{1,3},{2,4},{3,5}} }
};

results in the same way...

I googled for a lot of C-Examples to implement a hashtable in C. They had all one thing in common:
- to complicated for my purpose
- data-structure not initialized at load-time (what is necessary to port it to the flash-mem version afterwards)

So if anyone has a clue....

Greetings,
jabba

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure why you couldn't have just put all that into a complete test program and posted it here? Anyway I did:

#include 
#include 

/** \brief Table-Entry Data-Structure */
typedef struct{
    uint8_t key;
    uint8_t value;
}table_entry;

/** \brief Table Data-Structure */
typedef struct{
    uint8_t len;
    table_entry *tab;
}table;

table_entry entries[5] = {
    {5,1},
    {4,2},
    {1,3},
    {2,4},
    {3,5}
};

table tab = {
    5, entries
};

uint8_t *table_get_value(table *t, uint8_t key){
    uint8_t i = 0;
    while( i < (t->len) ){
        if(t->tab[i].key==key) return &(t->tab[i].value);
        i++;
    }
    return NULL;
};

int main(void) {
    PORTB = *(table_get_value(&tab, 1)); 
}

In the simulator that writes 3 to PORTB. That's exactly the value I would have expected so I'm not entirely sure what's wrong? (the 3 comes from the {1,3} entry in the table - the one with key value==1 which is what was asked for).

True I edited the code slightly to get rid of the warnings that came from your over-use of { }. I also added some white space around operators as I found it unreadable otherwise. But other than that this is your code and it works.

With the 4.7.x version of the compiler you now just need to added "__flash" and everything can easily be located into flash rather than the precious RAM where it currently resides in .data

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unbelievable!
This morning I ran the whole thing again and it just worked as intended. :D

Really don't know what's the difference to yesterday.
Anyway, thanks for your effort. I hope I will be able to return the favor some time.

Greetings,
jabba