修改现有char*或创建TMP char*?

发布于 2025-01-19 04:05:31 字数 952 浏览 3 评论 0原文

某些语言(例如 Java)要求您创建另一个字符串才能解决某些类型的问题。但是对于 C 语言,什么时候应该创建另一个字符串,什么时候应该简单地修改现有字符串?

以下面的代码为例:

    char *removeTags(char *s, int length)
{
    if (!s || length < 1)
    {
        exit(EXIT_FAILURE);
    }
    char *tmp = calloc(length, sizeof(char));
    int count = 0;
    for (int i = 0; i < length; i++)
    {
        if (s[i] == '<')
        {
            for (int k = i; k < length; k++)
            {
                if (s[k] == '>')
                {
                    i = k;
                    break;
                }
            }
        }
        else if (s[i] != '>' && s[i] != '<')
        {
            tmp[count] = s[i];
            count++;
        }
    }
    return tmp;
}

调用方式如下:

char *foo = (char *)calloc(length, sizeof(char)); foo = removeTags(foo, strlen(foo));

如果我只是修改 char *s 而不是创建 char *tmp 会更好吗帮我?

Some languages, like Java, require you to create another string in order to solve certain kinds of problems. But when it comes to C, when should you create another string and when should you simply modify an existing one?

take the following code as an example:

    char *removeTags(char *s, int length)
{
    if (!s || length < 1)
    {
        exit(EXIT_FAILURE);
    }
    char *tmp = calloc(length, sizeof(char));
    int count = 0;
    for (int i = 0; i < length; i++)
    {
        if (s[i] == '<')
        {
            for (int k = i; k < length; k++)
            {
                if (s[k] == '>')
                {
                    i = k;
                    break;
                }
            }
        }
        else if (s[i] != '>' && s[i] != '<')
        {
            tmp[count] = s[i];
            count++;
        }
    }
    return tmp;
}

it should be called as following:

char *foo = (char *)calloc(length, sizeof(char)); foo = removeTags(foo, strlen(foo));

would it be better if i just modified the char *s instead of creating the char *tmpto help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2025-01-26 04:05:31

如果该函数处理字符串,则第二个参数是冗余而容易出错的,

char *removeTags(char *s, int length)

如果要创建一个新的字符串,则必须声明该函数,就像

char * removeTags( const char *s );

该函数参数应具有限制符const

如果您想更改到位的字符串,则必须像注意那样

char * removeTags( char *s );

注意,您可能不会更改字符串文字。

如果您传递到函数还将字符串文字传递,则必须像定义

char * removeTags( const char *s );

两个函数一样声明该函数,但是在这种情况下,您需要使用不同的函数名称。

If the function deals with strings then the second parameter is redundant and error-prone

char *removeTags(char *s, int length)

If you want to create a new string then the function must be declared like

char * removeTags( const char *s );

That is the function parameter shall have the qualifier const.

If you want to change a string in place then the function must be declared like

char * removeTags( char *s );

Pay attention to that you may not change string literals.

If you pass to the function also string literals then the function must be declared like

char * removeTags( const char *s );

You could define the both functions but in this case you need to use different function names.

漆黑的白昼 2025-01-26 04:05:31

每当您在C函数中修改字符串时,就有一个真正的充分理由可以在使输入字符串参数a const char *时修改并返回副本。

您的呼叫者不会通过传递字符串文字来调用未定义的行为。这可能会用sigsegv之类的东西崩溃。

这样:

char *removeTags(const char *s);

通常,除非您需要限制正在操作的字符串范围,否则您无需为字符串操作传递长度。

另一个不太重要的原因:呼叫者可能无法使用字符串进行,并且可能需要未修改。

如果然后通过分析发现额外的副本步骤会导致性能或内存问题,则可以创建另一个功能,并称其为类似的功能:

void removeTagsInPlace(char *s);

请注意,返回值的差异使该字符串在原地进行了修改,无法错过。

Whenever you modify a string in a C function, there's one really good reason to make a copy of the string and modify and return the copy while making your input string argument a const char *.

Your caller won't invoke undefined behavior by passing in a string literal. That would likely crash your entire process with something like a SIGSEGV.

Like this:

char *removeTags(const char *s);

In general, you don't need to pass a length for string operations unless you need to limit the range of the string you're operating one.

Another, less important reason: the caller may not be done with the string and may need it unmodified.

If you then discover through profiling that the extra copy step causes performance or memory problem, then you can create another function, and call it something like this:

void removeTagsInPlace(char *s);

Note the difference in return value that makes the fact the string is modified in-place impossible to miss.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文