设置NULL会导致[MAX_STRING_LENGTH] = {'\ 0'};

时间:2019-05-17 16:25:15

标签: c++

我认为最好在我的代码中搜索..

之类的引用。
char buf[MAX_STRING_LENGTH]; 

...并将它们替换为...

char buf[MAX_STRING_LENGTH] = {'\0'};

在代码中进行搜索,我有一个数字设置为null(大约239),而其他数字则没有设置(1116)。

当我用char buf [MAX_STRING_LENGTH] = {'\ 0'};替换剩余的1,116个实例时;并将代码实时发布到游戏中,这显然太迟了。

还原更改消除了延迟。

有人可以解释为什么将它们设置为null会导致游戏在运行时滞后吗?

示例代码设置为Null

    void do_olist(Character *ch, char *argument, int cmd)
{
    int header = 1;
    int type = -1;
    int wear_bit = -1;
    int i = 0;
    int inclusive;
    int zone = -1;
    int yes_key1 = 0;
    int yes_key2 = 0;
    int yes_key3 = 0;
    int count = 0;
    Object *obj;
    bool found = false;
    char key1 [MAX_STRING_LENGTH] = {'\0'};
    char key2 [MAX_STRING_LENGTH] = {'\0'};
    char key3 [MAX_STRING_LENGTH] = {'\0'};
    char buf [MAX_STRING_LENGTH];

    argument = one_argument(argument, buf);

    if (!*buf)
    {
        ch->send("Selection Parameters:\n\n");
        ch->send("   +/-<object keyword>       Include/exclude object keyword.\n");
        ch->send("   <zone>                    Objects from zone only.\n");
        ch->send("   <item-type>               Include items of item-type.\n");
        ch->send("   <wear-bits>               Include items of wear type.\n");
        ch->send("\nExample:   olist +sword -rusty weapon 10\n");
        ch->send("will only get non-rusty swords of type weapon from zone 10.\n");
        return;
    }

    while (*buf)
    {

        inclusive = 1;

        if (strlen(buf) > 1 && isalpha(*buf) &&
                (type = index_lookup(item_types, buf)) != -1)
        {
            argument = one_argument(argument, buf);
            continue;
        }

        if (strlen(buf) > 1 && isalpha(*buf) &&
                (wear_bit = index_lookup(wear_bits, buf)) != -1)
        {
            argument = one_argument(argument, buf);
            continue;
        }

        if (isdigit(*buf))
        {

            if ((zone = atoi(buf)) >= MAX_ZONE)
            {
                ch->send("Zone not in range 0..99\n");
                return;
            }

            argument = one_argument(argument, buf);
            continue;
        }

        switch (*buf)
        {

            case '-':
                inclusive = 0;
            case '+':

                if (!buf [1])
                {
                    ch->send("Expected keyname after 'k'.\n");
                    return;
                }

                if (!*key1)
                {
                    yes_key1 = inclusive;
                    strcpy(key1, buf + 1);
                }
                else if (!*key2)
                {
                    yes_key2 = inclusive;
                    strcpy(key2, buf + 1);
                }
                else if (*key3)
                {
                    ch->send("Sorry, at most three keywords.\n");
                    return;
                }
                else
                {
                    yes_key3 = inclusive;
                    strcpy(key3, buf + 1);
                }

                break;

            case 'z':

                argument = one_argument(argument, buf);

                if (!isdigit(*buf) || atoi(buf) >= MAX_ZONE)
                {
                    ch->send("Expected valid zone after 'z'.\n");
                    return;
                }

                zone = atoi(buf);

                break;
        }

        argument = one_argument(argument, buf);
    }

    *b_buf = '\0';

    for (obj = full_object_list; obj; obj = obj->lnext)
    {
        if (zone != -1 && obj->zone != zone)
            continue;

        if (type != -1 && obj->obj_flags.type_flag != type)
            continue;

        if (wear_bit != -1)
        {
            for (i = 0; (*wear_bits[i] != '\n'); i++)
            {
                if (IS_SET(obj->obj_flags.wear_flags, (1 << i)))
                {
                    if (i != wear_bit)
                        continue;
                    else
                        found = true;
                }
            }
            if (found)
                found = false;
            else
                continue;
        }

        if (*key1)
        {
            if (yes_key1 && !strcasestr(const_cast<char*> (obj->getName().c_str()), key1))
                continue;
            else if (!yes_key1 && strcasestr(const_cast<char*> (obj->getName().c_str()), key1))
                continue;
        }

        if (*key2)
        {
            if (yes_key2 && !strcasestr(const_cast<char*> (obj->getName().c_str()), key2))
                continue;
            else if (!yes_key2 && strcasestr(const_cast<char*> (obj->getName().c_str()), key2))
                continue;
        }

        if (*key3)
        {
            if (yes_key3 && !strcasestr(const_cast<char*> (obj->getName().c_str()), key3))
                continue;
            else if (!yes_key3 && strcasestr(const_cast<char*> (obj->getName().c_str()), key3))
                continue;
        }

        count++;

        if (count < 200)
            olist_show(obj, type, header);

        header = 0;
    }

    if (count > 200)
    {
        sprintf(buf, "You have selected %d objects (too many to print all at once).\n",
                count);
        ch->send(buf);
        //return;
    }
    else {
            sprintf(buf, "You have selected %d objects.\n",
                count);
            ch->send(buf);
    }

    page_string(ch->desc, b_buf);
}

1 个答案:

答案 0 :(得分:0)

我建议更换...的实例

char buf[MAX_STRING_LENGTH] = {'\0'};

有...

char buf[MAX_STRING_LENGTH]; *buf = 0;