Hi All, I have implemented a linked list in my program on infineon sle66c168PE. at very first time i intialize the starting node with the null value and whenever i need to add the node to the linked list the malloc function return me the value 0x0000 that is a null value.which causes the program to fail. can anyone please suggest me something to work-around this problem... its very urgent ...Plz Thanks and Regards, Ankit
Did you call init_mempool to setup the heap? Jon
ankit, If you're just doing this as a learning exercise, that's fine, but you really should reconsider using malloc within an 8051 project that's to be sold to an end user. Malloc is useful in a PC or general purpose computing environment when you don't know how much RAM the system might contain. In the case of embedded systems, however, you typically know the total amount of RAM as well as how much of that RAM you can afford to devote to storing your data. What I'm saying is: Just make a static variable of a size appropriate to hold the maximum dataset you'll need to work with. Abandon malloc before you're in too deep.
"Malloc is useful in a PC or general purpose computing environment when you don't know how much RAM the system might contain." I'd rephrase that as 'Malloc is useful when you know that the PC (etc) contains substantially more memory than you're going to try and use'. In many situations the only reasonable thing one can do if malloc fails is to terminate the program, which is usually not a very reasonable thing to do from the user's perspective. "In the case of embedded systems, however, you typically know the total amount of RAM as well as how much of that RAM you can afford to devote to storing your data." Absolutely. "What I'm saying is: Just make a static variable of a size appropriate to hold the maximum dataset you'll need to work with." Or, if the program storage requirements allow it, try to use local variables so the compiler can overlay the data. Then you have a sort of compile time 'safe' malloc. It's all a bit like using an OS on an 8051 based system - there are probably some types of project where this is a reasonable thing to do, it's just that I can't think of one.
In the case of embedded systems, however, you typically know the total amount of RAM as well as how much of that RAM you can afford to devote to storing your data. I'd add that in addition embedded systems generally need to guarantee operation for certain specs. You can't just throw up an "out of memory" dialog box and quit back to the OS if you run out. You have to plan ahead of time and be sure that you will be able to allocate N foos simultaneously with M bars at run time. When malloc (and the like) does show up, it's generally part of some sort of base configuration code. Perhaps a system can support up to 32 foos, or 64 bars, or some combination where every foo costs you 2 bars, and the user can select that tradeoff. In that case, I might allocate a pool of foos and a pool of bars out of a shared memory area. I might even use malloc, though its overhead is generally unnecessary in this case, since the memory will never be freed (until the system is reconfigured). I commonly have code that dynamically allocates one item out of a table devoted to storing those things, but that's not the same as dynamic allocation from a generic shared, variable-sized memory heap, which brings lots of performance, fragmentation, and correctness questions, without adding much value.
simply: take that PC hat off and put a '51 hat on. Do not go by the urban legend "If you can do C, you can do any processor" Erik
"I have implemented a linked list" Note that it is not necessary to use dynamic memory allocation (malloc, etc) in order to use linked lists. You can allocate memory space statically (at compile time) and simply adjust the links at run time - eg, have a "free" and an "in-use" list. I think you should be getting the idea by now...
Do not go by the urban legend "If you can do C, you can do any processor" Amen !
yes i have used init_mempool to setup heap. i know its not feasable to use malloc in an embedded application but i know that my link list may contain 30 elements of 4 bytes each at max and i have enough RAM to accomodate all this. but the thing is even if i try to add the very first node still it returns null. this thing i could not understand and was thinking about this problem all night. Please suggest.... -Ankit
Is your heap large enough? Can you post a small example that can be compiled and that runs that shows the problem? Jon
yes i have used init_mempool to setup heap. i know its not feasable to use malloc in an embedded application but i know that my link list.. So now you know that it is also "not feasable to use" linked lists "in an embedded application" Set an array of pointer large enough to hold your max entries and take that darn PC hat off. Erik
" i know that my link list may contain 30 elements of 4 bytes each at max and i have enough RAM to accomodate all this." So don't mess about with malloc - define it all at compile time!
"So don't mess about with malloc - define it all at compile time!" Further explained here: http://www.eventhelix.com/RealtimeMantra/KeepItSimple.htm#Use Arrays