Reputation: 189
I am confused on what actually happens in memory when memset is called versus what happens when you call free.
For example I have a pointer A that points to an array of char*'s
char** A = (char**)calloc(5, sizeof(char*));
int i;
for(i=0;i<5;i++)
{
//filling
A[i] = (char*)calloc(30, sizeof(char));
scanf("%s", &A[i]);
}
now I want to reset it my char** pointer and all the elements it points to be completely empty
memset(A, 0, 5);
or
free(A);
what is the difference?
I am somewhat new to C so please speak in layman's terms thank you
Upvotes: 1
Views: 20405
Reputation: 708
While other answers explain the difference, let me add an example when both memset() and free() will need to be used together, in a specific order:
If the malloc'ed memory region was used to store any critical/valuable information that needs to be erased to prevent others from snooping on it (say some security-related stuff like managing a password or some other crypto), you would want to first erase the contents in that memory region and then call free() on it to give away that region's control back to the OS.
Hence, just like free() is the opposite of malloc(), memset(to zero)-then-free() is the opposite of calloc().
Upvotes: 0
Reputation: 409196
The memset
function sets an area of memory to the requested value. Do note that the size you provide is the number of bytes.
The free
function releases the allocated memory so it can't be used anymore. Calling free
doesn't usually modify the memory in any way. Using the memory after calling free
leads to undefined behavior.
Upvotes: 7
Reputation: 3450
memset() method just replaces the x memory bytes with a given character the allocated memory which is pointed by a pointer *a;
memset(a, 'a', x);
The prototype of memset() method is:
void* memset(void*, unsigned int, int);
memset()
behaves like strcpy()
but the difference is that memcpy()
copied the data as it is (byte), but strcpy
copies the formatted string as well (so takes more time than memcpy
to execute).
However, free()
method just deallocates the memory space and makes it available to get occupied.
Upvotes: 0
Reputation: 1267
memset
changes the contents at the memory address. It does not alter whether the memory is allocated/deallocated.
free
does not change the contents at the memory address. It deallocates the block of memory which makes it available for the program to reclaim and reuse. Therefore any pointers to this block become invalid and trying to access the memory should result in a Segfault ("if you're lucky" as my professor would say).
Use memset
when you know you are going to be accessing the data at that address again. Use free
when you know that the data will no longer be accessed ever again and that the program may reclaim that memory.
Upvotes: 0
Reputation: 137362
Both approaches are incorrect, but somewhat complementary.
memset
will set the content of the buffer to the given value, 0 in your case. This will change the value of the pointers, which will cause you to lose the references to the allocated buffers (in each A[i]
).
free(A)
will release the buffer pointed by A, but this buffer contains pointers, and each of the buffers that is pointed by them will not be freed.
in short - memset does not free a dynamically allocated buffer, and free does not set it to zero.
A correct approach will be something like that:
for(i=0;i<5;i++)
{
// complementary to
// A[i] = (char*)calloc(30, sizeof(char));
free(A[i]);
}
// complementary to
// char** A = (char**)calloc(5, sizeof(char*));
free(A);
A = NULL; // so no one gets confused...
Upvotes: 3
Reputation: 13892
free
deallocates the memory, which means A would still be pointing to the same memory location, which is invalid now.
memset
will set the memory currently pointed to by A, to whatever you want.
Upvotes: 1
Reputation: 7090
The difference is that memset
actually sets the value of a block of memory, while free
returns the memory for use by the operating system.
By analogy using physical things, memset(beer, 0, 6)
applied to a six-pack of beer would apply the value of '0' to all six members of the array beer
, while free(beer)
would be the equivalent of giving the six-pack away to a friend.
Upvotes: 12