I'm having a problem getting a linked list to work on Keil. I'm using an aduc842 microcontroller from Analog. Linked lists are something we do all the time in c and i'm baffled as to why i cant get it to work in this situation. This code works in the simulator but doesnt work on the actual hardware. Ive got 2k on chip xdata and it's configured as such. Ive tried using void * as well for the link node, which increases the size of the struct by one byte, and that didnt work either.
#include <ADuC842.h> sbit led = 0xb4; unsigned char xdata Store[1024] ; unsigned char xdata * pStore ; void xdata * FetchMem ( unsigned int bytes ) { unsigned char xdata * ppStore ; ppStore = pStore ; pStore += bytes ; return ppStore ; } xdata struct Example { unsigned char a ; struct Example xdata * Next ; } ; struct Example xdata List ; struct Example xdata * pList ; int main (void ){ unsigned char i ; FetchMem(1); pList=&List ; pList->a='a'; pList->Next = 0 ; { struct Example xdata * rec ; rec=FetchMem(sizeof(struct Example)); rec->a='b'; rec->Next=0; pList->Next=rec; pList=rec;} { struct Example xdata * rec ; rec=FetchMem(sizeof(struct Example)); rec->a='c'; rec->Next=0; pList->Next=rec; pList=rec; } { struct Example xdata * rec ; rec=FetchMem(sizeof(struct Example)); rec->a='d'; rec->Next=0; pList->Next=rec; pList=rec; } pList=&List; for(;;){ i=pList->a; if(pList->Next==0)break; pList=pList->Next; } led=!led; for(;;){} }
What does your program do or not do? You said it doesn't work, but didn't say how. Check memory initialization (and existence!) The simulator will init memory differently from real hardware. You should also be aware that the 8051 has many different address spaces (data, idata, xdata, etc). C assumes there's only one. C51 supports pointer types of varying sizes depending on where they point. There's also a "tagged" or "generic" pointer type that can point to any memory space, which carries an extra tag byte to denote where it points. (This is why your void* is one byte larger than your xdata*. An xdata* takes up 2 bytes and points into the 16-bit xdata address space. A generic void* might point anywhere, not just xdata.) As a result, "the" null pointer value is a bit tricky. There are actually many null pointer values. And they aren't necessarily "0". (C++ specifies that you can write the null pointer value name with the string "0". ANSI C never said so, hence the proliferation of #define NULL as ((void*)0) or ((size_t*)0L) or whatever.)