简体   繁体   中英

freeing a linked list

I have defined a linked list as follows

typedef struct uniquePattern{
  int pattern[*m]; 
  int position; 
  struct uniquePattern *previous;
  double backValues[]; 
}uniquePattern;

I add several elements to the list such that the first element has always previous = NULL. Then I try to free the memory using the next function.

  void free_ll(uniquePattern *head){
   uniquePattern *tmp;
    while (head != NULL){
     tmp = head;
     head = head->previous;
     free(tmp);
};

};

I get no errors. However in the examples of the textbook appears that the author free the contents of the linked list before freeing the linked list itself. For example:

typedef struct names{
  char *name;
  struct names *previous;
};

If name points to a string literal then may make sense to remove the string literal before removing the struct.

Is my code right? is my reasoning right?

EDIT:

double backValues[]; contains an array that increases lenght by one each time an element is added to the list. For example, if the user was adding coordinates of towns that he has visited and I need to calculate the euclidean distance then when the user adds a 4th town, backValues will be lenght 3, containing the distance between 4-1 4-2 and 4-3.

So I have a counter that knows how many elements are in the list, say, counter and each time I allocate a bit more memory when creating a new element. (counter-1)*sizeof(double) , for that array.

I malloc the array at the same time I malloc the struct (I do not call malloc twice).

You are correct in freeing the node. Many nodes will hold their data as pointers to other areas that have been malloc 'ed themselves. Judging from your comment, I think you know that. Looking at your structure, I'm a bit worried about double backValues[] .

However you specifically mention a string literal. No, you cannot free a string literal, or perhaps I should say that if you try then the effect is undefined. A string literal is normally held in read-only memory somewhere, not on the heap. You should only free something that has been created using malloc , calloc , or realloc .

But that might be semantics, if by "string literal" you mean an array of chars that has been created on the heap ( malloc , calloc , realloc ) then yes, it should be freed before the node. If you do not then you will loose track of the pointer, and you have a memory leak.

How is the memory allocated for backValues ? I think we should be told.

If you have pointers inside the structure that hold reference to some allocated memory chunk, you need to free them before you free the nodes of the list. For instance, assume a simple node definition:

typedef struct node_struct{
 int *arr;
 struct node_struct *next;
}node_t;

And you create a node:

node_t tmp_node;
node_t *node_ptr;
node_ptr = &tmp_node;

And you dynamically allocate space for the integer array "arr" as:

node_ptr->arr = malloc(ARR_SIZE * sizeof(int));

In this case, make sure you free the dynamically allocated array "arr" of the node before de-allocating the node itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM