This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Does C51 support 'Link List' data structure?

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.

Parents Reply Children
  • "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.