Updated April 13, 2023
Definition of Memset in C++
In C++, memset is a function that is used to fill the blocks of memory. It first converts the value of ‘c’ into an unsigned character and then copies that character to the first ‘n’ characters in the object pointed out by dest[] (fills the first ‘n’ blocks of dest[] with the same character ‘c’). The size ‘n’ mentioned in the memset should be smaller or equals to the size of the object pointed by dest[] otherwise it is undefined. If the object is non copyable, then also the behavior of the function is undefined. The function memset is defined in the <cstring> header file of C++.
Syntax:
Below given is the basic syntax of the memset function in the C++ program:
void *memset (void *dest, int c, size_t n);
where,
- dest[]: It defines a pointer to the object where character ‘c’ needs to be copied. Pointer to the memory which needs to be filled.
- c: It defines the character to be filled. The value passed is an ‘int’ type but it would be converted to unsigned char.
- n: It defines the number of times, the character ‘c’ needs to be copied. This value is an unsigned integral type.
- Return type: It returns the destpointer to which the values are copied using the memset function.
Working of Memset Function in C++
Some important points that need to be kept in mind about memset function are given below:
- memset function is used to copy the characters to fill the memory blocks.
- Conversion of ‘int’ to unsigned char takes place in the memset function before copying them into the array.
- The function shows undefined behavior if the count of the number of characters is greater than the size of the destination array.
- The function shows undefined behavior if the object is non- copyable (in case of arrays, structs, etc).
- We can set all the values to 0 or -1 using the memset function as it works bytes by bytes.
- Function memset is almost 10 times faster than the basic for a loop as it is optimized enough that it sets multiple bytes at a time whereas loop traverses through the whole array and performs the task.
- No overhead of memory freed is there for the programmer in the memset function as it does not allocate any memory which needs to be freed explicitly. It only fills the memory with some value given in the ‘c’ parameter.
- There is a lot of difference in memset and memcpy in terms of their basic tasks performed.
Examples of C++ Memset
Some of the examples showing the implementation of memset function in C++ program are given below:
Example #1
Replacing only a few blocks of character array using memset function.
Code:
// using the cstring library for memset function
#include<cstring>
#include<iostream>
using namespace std;
intmain() {
char arr_str[] = "Learning the advanced C++";
// fill only starting 10 blocks with the 'x' character
memset(arr_str, 'x', 10);
cout<< "Now the modified array is :" <<arr_str;
}
Output:
Explanation: In the above example, <cstring> header file is included to use the memset function. Character array ‘arr_str[]’ is initialized with the string. In order to fill only the first 10 characters with the ‘x’ character, memset function is used with the size value being passed as ‘10’.On printing the array on the console, in the modified string, only the first 10 characters are replaced as ‘x’ and after that, the string remain as it is.
Example #2
Using the sizeof() function to get the actual array size and setting 0 integral value by memset function.
Code:
#include<cstring>
#include<iostream>
using namespace std;
intmain() {
// initializing number array of size 8
intnum_arr[8];
// initializing character array having the text in it
char str_arr[]= "Hello we are learning memset function";
//using memset function to set all values to '0' in number array
memset(num_arr, 0, sizeof(num_arr));
cout<< "Values in number array are as follows: ";
//printing the number array by iterating it using for loop
for(inti = 0; i<8; i++)
{
cout<<num_arr[i] << " ";
}
cout<<endl;
//using memset function to set whole string 'y' in str_arr
memset(str_arr, 'y', sizeof(str_arr));
//printing modified string
cout<< "The new string is : " <<str_arr;
}
Output:
Explanation: In the above program, 2 arrays, i.e. character array (str_arr[]) and an integer array (num_arr[]) are initialized. For integral data type, we can set the values as 0 and -1 in num_arr[]. So we have set all the values of num_arr[] as 0 using the memset function. Function sizeof() is used to find the actual size of the array.In order to traverse the array and print its value on a console, for loop is used. In the character array, str_arr[], we have set the value ‘y’ for the whole array using the memset function.
Example #3
Undefined Behavior on inputting the number ‘n’ greater than the array length in memset function.
Code:
#include<cstring>
#include<iostream>
using namespace std;
intmain() {
// character array having the text in it
char str_arr[]= "Hello";
//using memset function to set whole string '*' in str_arr
memset(str_arr, '*', 15);
//printing modified string on the console
cout<< "The new string is : " <<str_arr;
}
Output:
Explanation: As explained above, the size ‘n’ passed in the memset function should be smaller or equal to the size of the array otherwise it would show undefined behavior. In the above program, the size of the character array ‘str_arr[]’ is 5. The value of ‘n’ passed in the memset function is 15, which is greater than the array size. So the results are undefined as you can see in the output.
Conclusion
The above description clearly explains what is memset in C++ and how it is used in the program to fill the blocks of memory with the desired value. Though this function is very useful and one of the easiest and fastest ways to fill the blocks of memory with the same value but in many cases, memset shows the undefined behavior, so one needs to be very careful and should be well aware of it before using it in programs.
Recommended Articles
This is a guide to C++ Memset. Here we also discuss the definition and working of memset function in c++ along with different examples and its code implementation. You may also have a look at the following articles to learn more –