Dear Expert, I have a simple C program with 'linklist' data structure processing as below. ////////////////////////////////////// #include <stdio.h> #include <stdlib.h> typedef struct node{ unsigned int id; struct node * next; }; void main(void){ struct node * first; struct node * n; unsigned int i; printf("/////////////////\r\n"); first->next = (struct node *) malloc(sizeof(struct node) * 10); if(first->next == NULL)printf("** Not enough memory space\r\n"); printf("first->next %X\r\n",first->next); first = NULL; //no first link for(i=1;i<=10;i++){ n->id = i; n->next = first; first = n; }; while(first != NULL){ printf("n->id = %X , n->next %X\r\n",first->id,first->next); first = first->next; } free(first); } ///////////////////// When I run this program , the result is unterminate loop. But I try to test this program on Turbo C++ when including "malloc.h" , it is work and result is correct! What wrong with this codeing? Then,I wonder does C51 support link list data processing? I will looking forward your reply and thank you for all suggestion. Birdy.
Without going into detail: You really don't want to use malloc(). Best find some other way of handling your data on the 8051. Stefan
"does C51 support link list data processing" Of course it does - there is nothing language-specific (let alone compiler-specific) about it! But have you correctly implemented the necessary support for dynamic memory allocation? Read the documentation for malloc in the Manual - including all the associated support fucntions! As has already been noted, dynamic memory allocation is not generally a good idea for embedded systems!
Looking at your code...
void main(void){ struct node * first; struct node * n; unsigned int i; printf("/////////////////\r\n"); first->next = (struct node *) malloc(sizeof(struct node) * 10);
"This probably writes over some important memory." like the stack, for instance...
like the stack, for instance... Or worse, Registers! Jon
both sound potentially disastrous to me!!
Hi, can you please extend and get into details, about why malloc use is not recommended ?
Hi Natahn, You didn't use init_mempool function. This may be the cause for your problems. Please read about it in the C51 compiler UG. Regards, Amit.
"Hi, can you please extend and get into details, about why malloc use is not recommended ?" Additional overhead in terms of code size, speed and RAM usage. If none of these things are an issue in your project then by all means use dynamic memory allocation, but I think it is rare to find an 8051 type project that isn't constrained by at least one of those things. Similar arguments apply when considering whether to use an operating system or to just code as god intended (I stole that quote from somewhere). A final point is that dynamic memory allocation pretty much enforces the use of pointers which offer the less experienced an excellent opportunity to introduce lots of hard to find bugs into their code.
"A final point is that dynamic memory allocation pretty much enforces the use of pointers which offer the less experienced an excellent opportunity to introduce lots of hard to find bugs into their code." Especially on the 8051, where pointers are more complicated due to having all those different address spaces... Having to access everything via pointers may also hit efficiency...
"why malloc use is not recommended ?" In addition to what Stefan's already said, think about the issue of fragmentation; ie, when your memory gets left with lots of little allocated areas scattered about...
"why malloc use is not recommended ?" Put it the other way around: for what reason would you actually need to use it? Unlike a PC, where the programmer usually has no idea how much RAM the final user's machine might have available, embedded systems usually have a well-defined, fixed amount of RAM (especially the type of systems appropriate to 8051s). Therefore, you are going to have to calculate the required size of your pool area, and set aside that much RAM at design time. So, if you have to lock-up that RAM at design time anyway, why bother with the overhead of dynamic allocation?! Of course, you might have a good reason to use it in your specific application - but that's why embedded systems usually avoid it.
'Link List', malloc(). You are clearly writing your program under the assumption the the '51 is "just another" processor. It is not, is is an "archtecturally different from most" processor. Keil has (out of kindness of reacting to customer pressure) added the necessary to use it as "just another", but that will lead to a dramatically underperforming (more hardware needed to do less) product. Erik
"'Link List', malloc()." Linked Lists do not inherently rely upon dynamic allocation, and can be quite useful even on an 8051; eg, http://www.8052.com/forum/read.phtml?id=70144 http://www.8052.com/forum/read.phtml?id=33232 "You are clearly writing your program under the assumption the the '51 is 'just another' processor. It is not, is is an 'archtecturally different from most' processor." Actually, the reasons why malloc() is generally avoided in embedded systems have nothing to do with the 8051 architecture - they apply equally to all the other processors that are not 'archtecturally different from most'
Linked Lists do not inherently rely upon dynamic allocation, and can be quite useful even on an 8051; useful, maybe, efficient no. A liked list is pointer to pointer to pointer to ..... which is not candy to a '51. why malloc() is generally avoided in embedded systems have nothing to do with the 8051 architecture agreed, it should be generally avoided, just as a lot of other "standard" things this is worse in a '51 than in most. I love the '51 and it performs beautifully when used as a '51, but when somone treat is as a "small PC" it sucks because it is so good at what is is intended for. Erik