C++ Memset 定义和使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13327155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:11:31  来源:igfitidea点击:

Memset Definition and use

c++cfunction

提问by Brandon Ling

What's the usefulness of the function memset()?.

函数有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 指向的内存块的前 num 个字节设置为指定值(解释为无符号字符)。

Does this mean it hard codes a value in a memory address?

这是否意味着它在内存地址中硬​​编码一个值?

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?

有人可以用非常简单的方式解释一下吗?

回答by Dietmar Kühl

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 lbytes starting at address bto the value c. It just does it much faster than in the above implementation.

也就是说,将从 address 开始memset(b, c, l)l字节设置b为 value c。它只是比上面的实现快得多。

回答by mattjgalloway

memset()is usually used to initialise values. For example consider the following struct:

memset()通常用于初始化值。例如,考虑以下结构:

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.

当然也可以用于其他场景,这只是其中之一。只需将其视为一种简单地将一部分内存设置为特定值的方法。

回答by AndersK

memsetis a common way to set a memory region to 0 regardless of the data type. One can say that memsetdoesn't care about the data type and just sets all bytes to zero.

memset无论数据类型如何,都是将内存区域设置为 0 的常用方法。可以说不memset关心数据类型,只是将所有字节设置为零。

IMHO in C++ one should avoid doing memsetwhen possible since it circumvents the type safety that C++ provides, instead one should use constructor or initialization as means of initializing. memset done on a class instance may also destroy something unintentionally:

恕我直言,在 C++ 中应该尽可能避免这样做memset,因为它规避了 C++ 提供的类型安全,而应该使用构造函数或初始化作为初始化的手段。在类实例上完成的 memset 也可能会无意中破坏某些东西:

e.g.

例如

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

a memseton an instance of the above would not do a reference counter decrement properly.

memset对上述的实例不会做一个参考计数器递减正常。

回答by Basile Starynkevitch

I guess that serv_addris some local or global variable of some structtype -perhaps struct sockaddr- (or maybe a class).

我想这serv_addr是一些一些局部或全局变量struct类型-perhaps struct sockaddr- (或者一个class)。

&serv_addris taking the address of that variable. It is a valid address, given as first argument to memset. The second argument to memsetis the byte to be used for filling (zero byte). The last argument to memsetis the size, in bytes, of that memory zone to fill, which is the size of that serv_addrvariable in your example.

&serv_addr正在获取该变量的地址。它是一个有效地址,作为 的第一个参数给出memset。to的第二个参数memset是用于填充的字节(零字节)。的最后一个参数memset是要填充的内存区域的大小(以字节为单位),即serv_addr示例中该变量的大小。

So this call to memsetclears a global or local variable serv_addrcontaining some struct.

所以这个调用memset清除serv_addr包含 some的全局或局部变量struct

In practice, the GCCcompiler, when it is optimizing, will generate clever code for that, usually unrolling and inlining it (actually, it is often a builtin, so GCCcan generate very clever code for it).

实际上,GCC编译器在优化时会为此生成巧妙的代码,通常是展开和内联它(实际上,它通常是内置函数,因此GCC可以为其生成非常巧妙的代码)。

回答by Vino

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;
}

回答by Ashit

memset- set bytes in memory

memset- 设置内存中的字节

Synopsis-

概要-

#include<string.h>

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

void *memset(void *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.Here for the above function , the memset() shall return s value.

说明- memset() 函数应将 c(转换为无符号字符)复制到 s 指向的对象的前 n 个字节中的每一个。对于上述函数,memset() 应返回 s 值。