c语言中memset
The memset() function in C is used to set blocks of memory with a particular value.
C语言中的memset()函数用于设置具有特定值的内存块。
In this article, we’ll have a look at how we can use this function in C programs.
在本文中,我们将看看如何在C程序中使用此函数。
This function takes a memory location, taken to be a void*
pointer. It then copies a byte (character) to the first n bytes pointed to by the memory location.
此函数占用一个内存位置,该位置是一个void*
指针。 然后,它将一个字节(字符)复制到存储位置所指向的前n个字节中。
Since it updates the memory location, it also returns a pointer to that updated memory location, which is again a void*
pointer.
由于它会更新内存位置,因此它还会返回一个指向该更新后的内存位置的指针,该指针还是void*
指针。
Therefore, we can write its prototype as:
因此,我们可以将其原型编写为:
void* memset(void* mem_loc, int c, size_t n);
Here, mem_loc
is the relevant memory location, and c
is the unsigned character. It sets the first n
bytes of mem_loc
.
在这里, mem_loc
是相关的内存位置,而c
是无符号字符。 它设置mem_loc
的前n
个字节。
Since this deals with characters and therefore strings (char*
), we get this function in the
header.
由于此函数处理字符,因此也处理字符串( char*
),因此可以在
标头中使用此函数。
We will now write the complete import and the function call.
现在,我们将编写完整的导入和函数调用。
#include
void* memset(void* mem_loc, int c, size_t n);
Let’s now look at examples regarding how we can use this function.
现在让我们看一下有关如何使用此功能的示例。
#include
#include
int main() {
char a[] = {"Hello from JournalDev"};
printf("a = %s\n", a);
printf("Filling the first 5 characters a with 'H' using memset\n");
memset(a, 'H', 5 * sizeof(char));
printf("After memset, a = %s\n", a);
return 0;
}
The above code snippet fills the first 5 characters of the string “Hello from JournalDev” with ‘H’. Let’s take a look at the output now:
上面的代码段用“ H”填充字符串“ Hello from JournalDev”的前5个字符。 现在让我们看一下输出:
a = Hello from JournalDev
Filling the first 5 characters a with 'H' using memset
After memset, a = HHHHH from JournalDev
As you can observe, the first 5 locations are indeed filled with ‘H’.
如您所见,前5个位置的确填充为“ H”。
Now let’s take another example, where you want to fill elements from an offset location.
现在让我们再举一个例子,您要在一个偏移位置填充元素。
#include
#include
int main() {
char a[] = {"Hello from JournalDev"};
printf("a = %s\n", a);
printf("Filling the last 5 characters a with 'H' using memset\n");
size_t a_len = strlen(a);
//Using an offset of (a + a_len - 5), so that we can
//fill the last 5 characters
memset(a + (a_len - 5), 'H', 5 * sizeof(char));
printf("After memset, a = %s\n", a);
return 0;
}
Output
输出量
a = Hello from JournalDev
Filling the last 5 characters a with 'H' using memset
After memset, a = Hello from JournHHHHH
This time, the last five characters are filled with ‘H’, since we specified the starting memory location address appropriately.
这次,由于我们适当地指定了起始存储位置地址,因此最后五个字符用“ H”填充。
A lot of times, we can use memset()
to zero initialize arrays. Often, the performance of memset()
is much faster than similar methods like calloc()
.
很多时候,我们可以使用memset()
将数组初始化为零。 通常, memset()
的性能比诸如calloc()
类似方法要快得多。
The below example illustrates this point while comparing the running time of both memset()
and calloc()
on a Linux Machine, using the
header file.
下面的示例使用
头文件比较Linux计算机上memset()
和calloc()
的运行时间,以此说明这一点。
#include
#include
#include
#include
void* init_with_memset(int* arr, size_t num_locations) {
// Perform zero initialization with memset
// on an integer array
return memset(arr, 0, num_locations * sizeof(int));
}
void* init_with_calloc(int* arr, size_t num_locations) {
arr = calloc(num_locations, sizeof(int));
return arr;
}
void* init_with_iteration(int* arr, size_t num_locations) {
// Naive unoptimized iteration using array indexing
for (int i=0; i
Output
输出量
Time for memset() = 0.000002 seconds
Time for calloc() = 0.000005 seconds
Time for naive iteration = 0.000006 seconds
As you can observe, memset()
is almost thrice as fast as both calloc()
and naive iteration, since it is optimized based on the architecture, beyond the C compiler!
如您所见, memset()
几乎是calloc()
和天真的迭代速度的三倍,因为它是基于C编译器之外的体系结构进行优化的!
We learned about how we can use memset()
to set values of a memory location. We also verified that the performance of memset()
is much better than the other functions since it is optimized as per the architecture.
我们了解了如何使用memset()
设置内存位置的值。 我们还验证了memset()
的性能比其他函数要好得多,因为它已根据体系结构进行了优化。
翻译自: https://www.journaldev.com/36863/memset-in-c
c语言中memset