简体   繁体   English

C - memset 与免费

[英]C - memset vs free

I am confused on what actually happens in memory when memset is called versus what happens when you call free.我对调用 memset 时内存中实际发生的情况与调用 free 时发生的情况感到困惑。

For example I have a pointer A that points to an array of char*'s例如,我有一个指向 char* 数组的指针 A

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现在我想重置它我的 char** 指针和它指向的所有元素完全为空

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我对 C 有点陌生,所以请用外行的话说谢谢

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.不同的是memset实际上是设置一块内存的值,而free返回内存供操作系统使用。

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.通过使用物理事物进行类比memset(beer, 0, 6)应用于六包啤酒的memset(beer, 0, 6)会将值 '0' 应用于数组beer所有六个成员,而free(beer)将相当于给把六包送给朋友。

The memset function sets an area of memory to the requested value. memset函数将内存区域设置为请求的值。 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. free函数释放分配的内存,因此不能再使用它。 Calling free doesn't usually modify the memory in any way.调用free通常不会以任何方式修改内存。 Using the memory after calling free leads to undefined behavior .在调用free后使用内存会导致未定义的行为

Both approaches are incorrect, but somewhat complementary.这两种方法都不正确,但有些互补。

memset will set the content of the buffer to the given value, 0 in your case. memset会将缓冲区的内容设置为给定的值,在您的情况下为 0。 This will change the value of the pointers, which will cause you to lose the references to the allocated buffers (in each A[i] ).这将更改指针的值,这将导致您丢失对已分配缓冲区的引用(在每个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. free(A)将释放 A 指向的缓冲区,但该缓冲区包含指针,并且它们指向的每个缓冲区都不会被释放。

in short - memset does not free a dynamically allocated buffer, and free does not set it to zero.简而言之 - memset 不会释放动态分配的缓冲区,并且 free 不会将其设置为零。

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...

free deallocates the memory, which means A would still be pointing to the same memory location, which is invalid now. free释放内存,这意味着 A 仍将指向相同的内存位置,这现在是无效的。

memset will set the memory currently pointed to by A, to whatever you want. memset会将 A 当前指向的内存设置为您想要的任何内容。

memset changes the contents at the memory address. memset更改内存地址处的内容。 It does not alter whether the memory is allocated/deallocated.它不会改变内存是否被分配/释放。

free does not change the contents at the memory address. free不会改变内存地址的内容。 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.当您知道将再次访问该地址的数据时,请使用memset Use free when you know that the data will no longer be accessed ever again and that the program may reclaim that memory.当您知道数据将不再被访问并且程序可能会回收该内存时,请使用free

memset() method just replaces the x memory bytes with a given character the allocated memory which is pointed by a pointer *a; memset() 方法只是用一个给定的字符替换 x 个内存字节,分配的内存由指针 *a 指向;

memset(a, 'a', x);

The prototype of memset() method is: memset() 方法的原型是:

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). memset()行为与strcpy()类似,但不同之处在于memcpy()按原样复制数据(字节),但strcpy复制格式化的字符串(因此执行时间比memcpy多)。

However, free() method just deallocates the memory space and makes it available to get occupied.然而, free()方法只是释放内存空间并使其可被占用。

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:虽然其他答案解释了差异,但让我添加一个示例,当 memset() 和 free() 需要按特定顺序一起使用时:

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.如果 malloc 的内存区域用于存储任何需要擦除以防止其他人窥探它的关键/有价值的信息(比如一些与安全相关的东西,如管理密码或其他一些加密),你会想要首先擦除该内存区域中的内容,然后对其调用 free() 以将该区域的控制权交还给操作系统。

Hence, just like free() is the opposite of malloc(), memset(to zero)-then-free() is the opposite of calloc().因此,就像 free() 是 malloc() 的对立面一样,memset(to zero)-then-free() 是 calloc() 的对立面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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