Home > Web Front-end > Front-end Q&A > Is there a memset function in JavaScript?

Is there a memset function in JavaScript?

WBOY
Release: 2022-03-24 14:17:21
Original
1983 people have browsed it

There is no memset() function in JavaScript; the memset() function is a "C/C" language initialization function. Its function is to set all the contents in a certain memory to a specified value. This function is usually for new applications. The memory is initialized, and this function cannot be used in JavaScript.

Is there a memset function in JavaScript?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Is there a memset function in JavaScript?

There is no memset() function in JavaScript

memset is the C/C language initialization function in the computer. Its function is to set all the contents in a certain memory to a specified value. This function usually initializes the newly requested memory.

void *memset(void *s, int ch, size_t n);
Copy after login

Function explanation: Replace the n bytes (typedef unsigned int size_t) following the current position in s with ch and return s.

memset: The function is to fill a given value in a memory block. It is the fastest way to clear a larger structure or array [1].

memset() function prototype is extern void *memset(void *buffer, int c, int count) buffer: is a pointer or array, c: is the value assigned to the buffer, count: is the length of the buffer .

Write a program below:

# include <stdio.h>
# include <string.h>
int main(void)
{
    int i;  //循环变量
    char str[10];
    char *p = str;
    memset(str, 0, sizeof(str));  //只能写sizeof(str), 不能写sizeof(p)
    for (i=0; i<10; ++i)
    {
        printf("%d\x20", str[i]);
    }
    printf("\n");
    return 0;
}
Copy after login

According to the different memset functions, the output results are also different, which are divided into the following situations:

memset(p, 0, sizeof(p));  //地址的大小都是4字节
0 0 0 0 -52 -52 -52 -52 -52 -52
memset(p, 0, sizeof(*p));  //*p表示的是一个字符变量, 只有一字节
0 -52 -52 -52 -52 -52 -52 -52 -52 -52
memset(p, 0, sizeof(str));
0 0 0 0 0 0 0 0 0 0
memset(str, 0, sizeof(str));
0 0 0 0 0 0 0 0 0 0
memset(p, 0, 10);  //直接写10也行, 但不专业
0 0 0 0 0 0 0 0 0 0
Copy after login

Related recommendations: javascript learning tutorial

The above is the detailed content of Is there a memset function in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template