memset() in C++

Video Tutorial
FREE
Functions thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

Overview

The memset() in C++ is used to assign a specific value to the contiguous memory blocks. It is useful for filling number of bytes with a given value starting from a specific memory location. The memset() function is defined in <cstring> header file.

Syntax

The syntax of memset() in C++ is:

Parameters of memset() in C++

The memset() in c++ takes the following three parameters.

  • obj: Pointer to the initial memory from where we want to add values, it can be a string name or pointer to string, array name or pointer to array, vector iterator, etc.
  • val: The value to be copied or added.
  • num: Number of bytes to be filled.

Return Values of memset() in C++

Return Type: The memset() in C++ returns obj, the pointer to the given object.

Example

Lets take a sample string as Str: C++ Sample Code

Output

The sample string Str initially has abcde at all indices. But after the memset() function, it has f at all indices.

Exceptions

The behavior of memset() in C++ is not similar in all the cases. It is undefined in the following cases:

  • When num is greater than the size of the object, it will have undefined behavior.
  • When the object is of array, vector, struct, etc, and value to be filled is other than 0 or -1, then it will show undefined behavior.

What is memset() in C++?

The memset() in C++ takes three arguments: obj, val, and num. The character represented by val is first converted to unsigned char and then copied into the first num bytes of the object pointed by the obj pointer.

The memset() in C++ for int array will copy the given value at all the indices of the array and same for the vector. The behavior of memset() function for char array is explained in first example.

More Examples

Example 1:

Lets take a vector vec:

C++ Sample Code

Output

Explanation:

  • The memset() in C++ can be used for vectors as mentioned above. Vector vec is initialized with 5.
  • Using memset(&vec[2], 0, sizeof(vec[2]) * (vec.size()-3)); in this the starting address passed is &vec[2]; i.e., address of 2nd index. The value to be filled is 0, and number of bytes passed is sizeof(vec[2]) * (vec.size()-3), that is (size of one block) *(6 - 3). It will fill value 0 till 3 blocks starting from index 2.

Example 2:

As we have seen this in detail in above section, memset function on an array can be used only to fill values as 0 or -1; otherwise, it will not work for an array. Let's see with the following demonstration.

Output:

Explanation: We can see from the above case that the array can be initialized using memset() with 0 and -1 only.

Conclusion

  • The memset() in C++ is used to assign a specific value to the contiguous memory blocks.
  • When number of bytes to be filled is greater than the size of the object, memset() in C++ will have undefined behavior.
  • When the object is of array, struct, etc. and value to be filled is other than 0 or -1, then memset() in C++ will show undefined behavior.
  • Memset() in C++ for integral data types(array, vector, etc.), only two values, 0 and -1, are allowed.
  • In memset() in C++, if the object is of string type, any character can be copied to all the indices.

See Also: