Open In App

Memset in C++

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ memset() is a function that copies a single character for a specified number of times to the given bytes of memory. It is useful for filling a number of bytes with a given value starting from a specific memory location.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char str[5];
    memset(str, 't', sizeof(str));
    cout << str;
    return 0;
}

Output
ttttt

In the above program, we declares a character array str of size 5, and then the memset function is used to initialize each element of the array with the character 't'.

Syntax of memset()

The memset() function is defined inside <cstring> header file.

C++
memset(str,ch, n);

Parameters

  • str[]: Pointer to the object to copy the character.
  • ch: The character to copy. It can be a character, a normal value as well a Boolean value.
  • n: Number of bytes to copy.

Return Value

  • The memset() function returns str, the pointer to the destination string.

std::memset() converts the value ch to unsigned char and copies it into each of the first n characters of the object pointed to by str[]. If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), the behavior is undefined. If n is greater than the size of the object pointed to by str, the behaviour is undefined. 

Examples of memset()

The following examples demonstrates the use of memset() functions in C++:

Character Array Initialization

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    char str[] = "geeksforgeeks";
    
    // setting each byte of memory 
    // of str to 't' using memset
    memset(str, 't', sizeof(str));
    cout << str;
    return 0;
}

Output
tttttttttttttt

In the above code, memset() function takes O(sizeof(str) time.

Initialize Integer Array

We can use memset() to set all values as 0 or -1 for integral data types also. It will not work if we use it to set as other values. The reason is simple, memset works byte by byte.

CPP
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a[5];

    // memset() with value (0)
    memset(a, 0, sizeof(a));
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
        cout << endl;
    
    // memset() with value (1)
    memset(a, -1, sizeof(a));
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
    cout << endl;

    // memset() with value (5)
    memset(a, 5, sizeof(a));
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
}

Output
0 0 0 0 0 
-1 -1 -1 -1 -1 
84215045 84215045 84215045 84215045 84215045 

In the above program, we observe that using values like 0 or -1 with memset() works as expected. However, when setting a different value, such as 5, the result may not be as expected due to memset() operates at the byte level.

Initialize Boolean Array

CPP
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    bool prime[5];
    memset(prime, true, sizeof(prime));
    
    // If you print without using 
    // boolalpha it will print like this
    for (int i = 0; i < 5; i++)
        cout << prime[i] << " ";
        cout << endl;
    
    // If you use boolalpha it will 
    // print like this
    for (int i = 0; i < 5; i++)
        cout << boolalpha << prime[i] << " ";
    return 0;
}

Output
1 1 1 1 1 
true true true true true 

In the above program, When we print the array without using boolalpha, the output will be 1 1 1 1 1 because boolean values are represented as 1 for true and 0 for false by default. However, when we use boolalpha, the output will be true true true true true, as boolalpha changes the format of the output to display boolean values as the words true and false instead of 1 and 0.

Note: For boolean, the object must be of bool type for C++. Eg. bool arr[n];

Advantages of std::memset( )

  1. It is a one-line piece of code for initialization of large blocks of memory like array to a specific value, making it highly compact, fast and improving readability overall.
  2. memset() is a C function that works well in C++ as well, which makes it useful when working with codebases that involve both C and C++.
  3. By using memset(), you ensure that uninitialized memory is properly set, preventing potential issues with undefined or garbage values.
  4. The C++ memset() function aids the programmer in solving the misalignment issue. There are instances where you discover that the processor is having trouble with data alignment, which results in a programming mistake. The memcpy() and memcmp() methods in C++ are the best options in this situation.

Limitations of std::memset()

  1. memset() operates at the byte level, and its use is not type-safe. It treats the memory block as a sequence of bytes.
  2. For non-POD (Non - Plain Old Data) types in C++, such as classes with constructors, memset should be used with caution. Initializing such objects using memset can bypass constructors and lead to undefined behaviour. Instead, constructors should be used for initialization.

The memset() function is inherited from C. It is not very flexible when filling memory with different data types and value. The std::fill function is better alternative available in STL Algorithm library.


Next Article

Similar Reads