C++ memset
The memset function in C++ is used to set a block of memory to a specific value. It is defined in the <cstring> header file.
The syntax of memset is as follows:
void* memset(void* ptr, int value, size_t num);
Here, ptr is a pointer to the block of memory that you want to set, value is the value that you want to set each byte of the memory block to, and num is the number of bytes to be set.
The memset function is commonly used to initialize arrays or to set a block of memory to zero. It is often used in conjunction with the sizeof operator to specify the number of bytes to be set.
Here is an example usage of memset:
#include <iostream>
#include <cstring>
int main() {
int arr[5];
// Set the entire array to zero
memset(arr, 0, sizeof(arr));
// Print the array
for (int i = 0; i < 5; i++) {
std::cout << arr[i] << " ";
}
return 0;
}
Output:
0 0 0 0 0
In this example, the memset function is used to set each element of the arr array to zero. The sizeof operator is used to determine the number of bytes in the array.
原文地址: https://www.cveoy.top/t/topic/hWTE 著作权归作者所有。请勿转载和采集!