简体   繁体   English

Memset 定义和使用

[英]Memset Definition and use

What's the usefulness of the function memset() ?. function memset()有什么用处?

Definition : Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).定义:将ptr指向的memory块的前num字节设置为指定值(解释为无符号字符)。

Does this mean it hard codes a value in a memory address?这是否意味着它对 memory 地址中的值进行硬编码?

memset(&serv_addr,0,sizeof(serv_addr) is the example that I'm trying to understand. memset(&serv_addr,0,sizeof(serv_addr)是我试图理解的例子。

Can someone please explain in a VERY simplified way?有人可以用非常简单的方式解释吗?

memset() is a very fast version of a relatively simple operation: memset()是相对简单操作的非常快速的版本:

void* memset(void* b, int c, size_t len) {
    char* p = (char*)b;
    for (size_t i = 0; i != len; ++i) {
        p[i] = c;
    }
    return b;
}

That is, memset(b, c, l) set the l bytes starting at address b to the value c . 也就是说, memset(b, c, l)将从地址b开始的l个字节设置为值c It just does it much faster than in the above implementation. 它的执行速度比上述实现要快得多。

memset() is usually used to initialise values. memset()通常用于初始化值。 For example consider the following struct: 例如,考虑以下结构:

struct Size {
    int width;
    int height;
}

If you create one of these on the stack like so: 如果您像这样在堆栈上创建其中之一:

struct Size someSize;

Then the values in that struct are going to be undefined. 然后该结构中的值将是不确定的。 They might be zero, they might be whatever values happened to be there from when that portion of the stack was last used. 它们可能为零,也可能是上次使用那部分堆栈时发生的任何值。 So usually you would follow that line with: 所以通常您会遵循以下原则:

memset(&someSize, 0, sizeof(someSize));

Of course it can be used for other scenarios, this is just one of them. 当然,它也可以用于其他情况,这只是其中之一。 Just think of it as a way to simply set a portion of memory to a certain value. 只需将其视为简单地将一部分内存设置为某个值的方法即可。

memset is a common way to set a memory region to 0 regardless of the data type. memset是一种将内存区域设置为0的常用方法,而与数据类型无关。 One can say that memset doesn't care about the data type and just sets all bytes to zero. 可以说memset不在乎数据类型,而只是将所有字节设置为零。

IMHO in C++ one should avoid doing memset when possible since it circumvents the type safety that C++ provides, instead one should use constructor or initialization as means of initializing. C ++中的IMHO应该避免在可能的情况下执行memset ,因为它会绕过C ++提供的类型安全,而应该使用构造函数或初始化作为初始化的手段。 memset done on a class instance may also destroy something unintentionally: 在类实例上完成的memset可能还会无意中破坏某些内容:

eg 例如

class A
{
public:
  shared_ptr<char*> _p;
};

a memset on an instance of the above would not do a reference counter decrement properly. 上面实例的memset不能正确执行参考计数器递减。

I guess that serv_addr is some local or global variable of some struct type -perhaps struct sockaddr - (or maybe a class ). 我猜serv_addr是某些struct类型的本地变量或全局变量-也许是struct sockaddr (或者可能是一个class )。

&serv_addr is taking the address of that variable. &serv_addr正在获取该变量的地址。 It is a valid address, given as first argument to memset . 这是有效的地址,作为memset第一个参数给出。 The second argument to memset is the byte to be used for filling (zero byte). memset的第二个参数是用于填充的字节(零字节)。 The last argument to memset is the size, in bytes, of that memory zone to fill, which is the size of that serv_addr variable in your example. memset的最后一个参数是要填充的内存区域的大小(以字节为单位),它是示例中该serv_addr变量的大小。

So this call to memset clears a global or local variable serv_addr containing some struct . 因此,此对memset调用将清除包含某些struct的全局或局部变量serv_addr

In practice, the GCC compiler, when it is optimizing, will generate clever code for that, usually unrolling and inlining it (actually, it is often a builtin, so GCC can generate very clever code for it). 实际上, GCC编译器在进行优化时会为此生成聪明的代码,通常会对其进行展开和内联(实际上,它通常是内置的,因此GCC可以为其生成非常聪明的代码)。

It is nothing but setting the memory to particular value. 就是将内存设置为特定值。

Here is example code. 这是示例代码。

Memset(const *p,unit8_t V,unit8_t L) , Here the P is the pointer to target memory, V is the value to the target buffer which will be set to a value V and l is the length of the data. Memset(const * p,unit8_t V,unit8_t L),这里P是指向目标内存的指针,V是指向目标缓冲区的值,该值将被设置为值V,而l是数据的长度。

while(L --> 0)
{
    *p++ = V;
}

memset- set bytes in memory memset-设置内存中的字节

Synopsis- 概要-

#include<string.h>

void *memset(void *s,int c,size_t n) 无效* memset(无效* s,int c,size_t n)

Description- The memset() function shall copy c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s. 描述-memset()函数应将c(转换为无符号字符)复制到s所指向的对象的前n个字节中的每个字节中。 Here for the above function , the memset() shall return s value. 对于上述函数,memset()将返回s的值。

instead of adding overhead of routine call to memset, serv_addr = {0}; serv_addr = {0};而不是增加对 memset 的例行调用开销; can also be done也可以做

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

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