Memset Definition and use

C++CFunction

C++ Problem Overview


What's the usefulness of the 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).

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.

Can someone please explain in a VERY simplified way?

C++ Solutions


Solution 1 - C++

memset() is a very fast version of a relatively simple operation:

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. It just does it much faster than in the above implementation.

Solution 2 - C++

memset() is usually used to initialise values. 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.

Solution 3 - C++

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

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. memset done on a class instance may also destroy something unintentionally:

e.g.

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

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

Solution 4 - C++

I guess that serv_addr is some local or global variable of some struct type -perhaps struct sockaddr- (or maybe a class).

&serv_addr is taking the address of that variable. It is a valid address, given as first argument to memset. The second argument to memset is the byte to be used for filling (zero byte). 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.

So this call to memset clears a global or local variable serv_addr containing some struct.

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

Solution 5 - C++

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.

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

Solution 6 - C++

memset- set bytes in memory

Synopsis-

#include<string.h>

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrandon LingView Question on Stackoverflow
Solution 1 - C++Dietmar KühlView Answer on Stackoverflow
Solution 2 - C++mattjgallowayView Answer on Stackoverflow
Solution 3 - C++AndersKView Answer on Stackoverflow
Solution 4 - C++Basile StarynkevitchView Answer on Stackoverflow
Solution 5 - C++VinoView Answer on Stackoverflow
Solution 6 - C++AshitView Answer on Stackoverflow