-2

So I'm going through a piece of code in one of the programs and it uses an unlink function , where

hist_name = malloc(128)

However, I did notice that the program did not use a "free" to free the memory , but it did have the unlink function shown below, at the end of the program:

unlink(hist_name);

My question is , does the unlink function free memory in addition to deleting the link to the file, or do I still need to insert a free(hist_name) statement to free the memory?

Mandi
  • 41
  • 5
  • 2
    Re “deleting the file name”: `unlink` removes a link to a **file**, which will result in deleting the file if there are no other links to it and no processes have the file open. (Files can be linked to from multiple directories.) It does not delete a **file name**. If you are going to work with computers, it is important to be specific and clear with concepts. – Eric Postpischil Oct 24 '20 at 11:53
  • `unlink` cannot free the memory at `hist_name` because the string may not have been dynamically allocated at all; it may, for example, be a string literal. There is no (portable) way to tell from just an address how that memory has been allocated. – Peter - Reinstate Monica Oct 24 '20 at 12:01

4 Answers4

2

unlink does not free memory. If you want the memory pointed to by hist_name released, you should pass the address to free.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

unlink() deletes a file. More specifically it unlinks the given name from the content on disk, and when there are no more names linked to some content, the content is automatically reclaimed (i.e. the file is truly deleted).

It does not free any memory. Use free() for that (after unlink() of course). But freeing memory right before your program ends is not critical on typical operating systems, which will automatically reclaim all memory which was used by a process when it terminates.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

No. hist_name is leaked. unlink won't free the parameter.

Create a file called "a" and run this code (Compile with gcc test.c):

#include <unistd.h>
#include <stdlib.h>

int main(void){
    char* hist_name=malloc(128);
    //Fill hist_name
    hist_name[0]='a';
    hist_name[1]='\0';
    unlink(hist_name);
}

In the following, to show you, that unlink doesn't free your allocated memory, I will use valgrind. Valgrind is a tool that allows you to detect memory leaks and other memory issues (Like out-of-bounds accesses, use of uninitialized values,etc.)

So, if you run this program with valgrind (valgrind ./a.out), you get this output:

==2155== Memcheck, a memory error detector
==2155== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2155== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==2155== Command: ./a.out
==2155== 
==2155== 
==2155== HEAP SUMMARY:
==2155==     in use at exit: 128 bytes in 1 blocks
==2155==   total heap usage: 1 allocs, 0 frees, 128 bytes allocated
==2155== 
==2155== LEAK SUMMARY:
==2155==    definitely lost: 128 bytes in 1 blocks
==2155==    indirectly lost: 0 bytes in 0 blocks
==2155==      possibly lost: 0 bytes in 0 blocks
==2155==    still reachable: 0 bytes in 0 blocks
==2155==         suppressed: 0 bytes in 0 blocks
==2155== Rerun with --leak-check=full to see details of leaked memory
==2155== 
==2155== For lists of detected and suppressed errors, rerun with: -s
==2155== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

definitely lost: 128 bytes in 1 blocks

This means, you allocated memory once, 128 bytes, but didn't free it. ==> unlink doesn't free your memory for you.

JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
0

Short answer - No.

The unlink function only changes the file system - not the heap or the allocated memory.

Yuval Zilber
  • 131
  • 11