简体   繁体   English

memset在C语言中的作用是什么

[英]What is the purpose of memset in C

I am reading REBOL source code and I can't understand the purpose of the following statement: 我正在阅读REBOL源代码,但无法理解以下语句的目的:

/***********************************************************************
**
*/  int main(int argc, char **argv)
/*
***********************************************************************/
{
    char *cmd;

    // Parse command line arguments. Done early. May affect REBOL boot.
    Parse_Args(argc, argv, &Main_Args);

    Print_Str("REBOL 3.0\n");

    REBOL_Init(&Main_Args);

    // Evaluate user input:
    while (TRUE) {
        cmd = Prompt_User();
        REBOL_Do_String(cmd);
        if (!IS_UNSET(DS_TOP)) {
            //if (DSP > 0) {
                if (!IS_ERROR(DS_TOP)) {
                    Prin("== ");
                    Print_Value(DS_TOP, 0, TRUE);
                } else
                    Print_Value(DS_TOP, 0, FALSE);
            //}
        }
        //DS_DROP; // result
    }

    return 0;
}

In Parse_Args function: 在Parse_Args函数中:

/***********************************************************************
**
*/  void Parse_Args(int argc, REBCHR **argv, REBARGS *rargs)
/*
**      Parse REBOL's command line arguments, setting options
**      and values in the provided args structure.
**
***********************************************************************/
{
    REBCHR *arg;
    REBCHR *args = 0; // holds trailing args
    int flag;
    int i;

    CLEARS(rargs);

    ....

And CLEARS is defined: CLEARS定义为:

#define CLEARS(m)       memset((void*)(m), 0, sizeof(*m));

So my question is why memset is being used here? 所以我的问题是为什么在这里使用memset

It looks like rargs is some kind of struct containing options for the program. 看起来rargs是某种包含程序选项的结构。 CLEARS() and memset() is used to fill that struct with zero values to initiate it. CLEARS()memset()用于用零值填充该结构以启动它。

memset sets a block of memory to a given value. memset将内存块设置为给定值。 It is normally written in hand optimized assembly and is expected to be blindingly fast. 它通常是用手工优化的程序集编写的,并且有望以惊人的速度实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM