0

I want to know please where i have to use the "free" function?

does I use it when i already use the "malloc" function or I use it with any declared pointer?

Note: in the two cases the liste type is defined as below:

typedef struct Node * liste; 

Node is a also a structure:

struct Node { 
    int value; 
    Node *N;
};

First case

liste l; 
free (l);

second case

liste l;
l=(node*)malloc(sizeof(node));
free (l);

thank you in advance !

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 3
    What does your book say on the topic? This should be covered fairly early on. – Lightness Races in Orbit Feb 25 '19 at 00:04
  • 1
    In the first case no memory was allocated so none can be `free`d. In the second case you are [casting the return value from `malloc'](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) but it is a different type. Are you hiding a pointer behind a type definition? – Weather Vane Feb 25 '19 at 00:10
  • 1
    @WeatherVane for the second case, the liste type is defined as bellow: typedef struct Node * liste; and Node is a also a structure. struct Node { int value; Node*N;} – Constellation Feb 26 '19 at 22:06
  • 1
    @Constellation [Edit your question](https://stackoverflow.com/posts/54857762/edit) to add this information. – dbush Feb 26 '19 at 22:18
  • Possible duplicate of [How do malloc() and free() work?](https://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work) – Dustin Nieffenegger Feb 26 '19 at 22:38

1 Answers1

1

The man page for free states the following:

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

In your first case, the use of free is invalid because l is not initialized. The second case is valid (assuming liste is a typedef for a pointer) because l is assigned an address returned by malloc.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • in the first case, the liste type is defined as bellow: Node * liste; and Node is a also a structure. struct Node { int value; Node*N;} – Constellation Feb 26 '19 at 22:12
  • 1
    @Constellation Then this answer stands. Also, add that information to your question. – dbush Feb 26 '19 at 22:14
  • done but i dont understand yet the use of free. if p is a pointer --> we can apply the "free" function or we have, imperatively, to use the "malloc" function, to use then, the "free" function?. thank you in advance . – Constellation Feb 26 '19 at 22:28
  • 1
    @Constellation `free` is used to de-allocate memory that was allocated by `malloc`, as stated in the quoted passage. – dbush Feb 26 '19 at 22:30
  • so can not be used in the first cas? – Constellation Feb 26 '19 at 22:36
  • @Constellation No, because `l` was never set to anything. – dbush Feb 26 '19 at 22:37
  • and if a set it to an already created linked list. liste a; l=a->N; can i use "free(l);" if yes, which memory will be de-allocated?thank you a lot. – Constellation Feb 26 '19 at 22:41
  • @Constellation It depends on what was assigned to `a->N`. If it was memory allocated by `malloc` then yes, otherwise no. – dbush Feb 26 '19 at 22:42
  • i understand and if we have two pointers which point on a linked list, the size of the list=3 elements. " liste a, b;" memory is allocated using "malloc". if we use "free(b)", the memory allocated for the linked list will be de-allocated? --> the " a" pointer will point on Null? – Constellation Feb 26 '19 at 22:51
  • @Constellation freeing a pointer does not set the pointer value to NULL. You still have to do that yourself. – dbush Feb 26 '19 at 23:13
  • Dear @dbush, i mean when i have two pointers pointers the same linked list or a dynamic table. if I free a pointer, the pointed structure will be lost or i can always have it using the other pointer? – Constellation Feb 26 '19 at 23:23
  • @Constellation once you free the memory it is invalid. Any it is undefined behavior to use a pointer that points to memory that has been freed. – dbush Feb 26 '19 at 23:54