c语言中memset_C中的memset()

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程序中使用此函数。



C中memset()的语法 (Syntax of memset() in 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个字节。

相关头文件 (Relevant Header Files)

Since this deals with characters and therefore strings (char*), we get this function in the <string.h> header.

由于此函数处理字符,因此也处理字符串( char* ),因此可以在<string.h>标头中使用此函数。

We will now write the complete import and the function call.

现在,我们将编写完整的导入和函数调用。


#include <string.h>

void* memset(void* mem_loc, int c, size_t n);

Let’s now look at examples regarding how we can use this function.

现在让我们看一下有关如何使用此功能的示例。

在C中使用memset()函数 (Using the memset() function in C)


#include <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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”填充。



memset()vs calloc()vs天真的迭代 (memset() vs calloc() vs naive iteration)

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 <time.h> header file.

下面的示例使用<time.h>头文件比较Linux计算机上memset()calloc()的运行时间,以此说明这一点。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

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<num_locations; i++) {
        arr[i] = 0;
    }
    return arr;
}

int main() {
    // Set the array to -1 initially
    int arr[2560];
    for (int i=0; i<2560; i++)
        arr[i] = -1;

    clock_t start_time, end_time;
    double total_time;

    start_time = clock();
    // 1000 locations
    init_with_memset(arr, 1000);
    end_time = clock();
    total_time  =  (double) (end_time - start_time);
    total_time =   total_time / CLOCKS_PER_SEC;
    printf("Time for memset() = %.6f seconds\n", total_time);
    
    start_time = clock();
    // 1000 locations
    init_with_calloc(arr, 1000);
    end_time = clock();
    total_time  =  (double) (end_time - start_time);
    total_time =   total_time / CLOCKS_PER_SEC;
    printf("Time for calloc() = %.6f seconds\n", total_time);
    
    start_time = clock();
    // 1000 locations
    init_with_iteration(arr, 1000);
    end_time = clock();
    total_time  =  (double) (end_time - start_time);
    total_time =   total_time / CLOCKS_PER_SEC;
    printf("Time for naive iteration = %.6f seconds\n", total_time);

    return 0;
}

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编译器之外的体系结构进行优化的!



结论 (Conclusion)

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()的性能比其他函数要好得多,因为它已根据体系结构进行了优化。

参考资料 (References)



翻译自: https://www.journaldev.com/36863/memset-in-c

c语言中memset

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值