将 char** 转换为 char* 或 char

发布于 2024-07-14 12:54:06 字数 249 浏览 7 评论 0原文

我有一个旧程序,其中使用了一些库函数,但我没有该库。

所以我正在使用 C++ 库编写该程序。 在旧代码中,有一些函数被调用,如下所示

*string = newstrdup("Some string gone here");

字符串变量声明为 char **string;

他可能在名为“newstrdup”的函数中做什么? 我尝试了很多事情,但我不知道他在做什么......任何人都可以帮忙吗

I have a old program in which some library function is used and i dont have that library.

So I am writing that program using libraries of c++.
In that old code some function is there which is called like this

*string = newstrdup("Some string goes here");

the string variable is declared as char **string;

What he may be doing in that function named "newstrdup" ?
I tried many things but i dont know what he is doing ... Can anyone help

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

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

发布评论

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

评论(6

温柔戏命师 2024-07-21 12:54:06

该函数用于复制 C 字符串。 通常需要获取字符串文字的可写版本。 它们(字符串文字)本身不可写,因此这样的函数将它们复制到分配的可写缓冲区中。 然后,您可以将它们传递给修改给定参数的函数,例如 strtok 写入它必须标记的字符串。

我认为你可以想出这样的东西,因为它被称为newstrdup

char * newstrdup(char const* str) {
    char *c = new char[std::strlen(str) + 1];
    std::strcpy(c, str);
    return c;
}

一旦使用完成,你应该释放它string using

delete[] *string;

另一种编写方法是使用malloc。 如果库很旧,它可能使用了 C++ 从 C 继承的库:

char * newstrdup(char const* str) {
    char *c = (char*) malloc(std::strlen(str) + 1);
    if(c != NULL) {
        std::strcpy(c, str);
    }
    return c;
}

现在,您应该在完成后使用 free 释放字符串:

free(*string);

如果您使用 C++ 编写,请首选第一个版本。 但如果现有代码使用free再次释放内存,请使用第二个版本。 请注意,如果没有内存可用于复制字符串,第二个版本将返回 NULL,而第一个版本在这种情况下会引发异常。 还应注意将 NULL 参数传递给 newstrdup 时的行为。 取决于您的图书馆,可能允许或不允许。 因此,如有必要,请在上述函数中插入适当的检查。 POSIX 系统中有一个名为 strdup 的函数,但该函数既不允许 NULL 参数,也不使用 C++ 运算符 new 来分配内存。

不管怎样,我用 google codesearch 查找了 newstrdup 函数并发现了很多。 也许您的图书馆就在搜索结果中:

Google CodeSearch ,newstrdup

The function is used to make a copy of c-strings. That's often needed to get a writable version of a string literal. They (string literals) are itself not writable, so such a function copies them into an allocated writable buffer. You can then pass them to functions that modify their argument given, like strtok which writes into the string it has to tokenize.

I think you can come up with something like this, since it is called newstrdup:

char * newstrdup(char const* str) {
    char *c = new char[std::strlen(str) + 1];
    std::strcpy(c, str);
    return c;
}

You would be supposed to free it once done using the string using

delete[] *string;

An alternative way of writing it is using malloc. If the library is old, it may have used that, which C++ inherited from C:

char * newstrdup(char const* str) {
    char *c = (char*) malloc(std::strlen(str) + 1);
    if(c != NULL) {
        std::strcpy(c, str);
    }
    return c;
}

Now, you are supposed to free the string using free when done:

free(*string);

Prefer the first version if you are writing with C++. But if the existing code uses free to deallocate the memory again, use the second version. Beware that the second version returns NULL if no memory is available for dup'ing the string, while the first throws an exception in that case. Another note should be taken about behavior when you pass a NULL argument to your newstrdup. Depending on your library that may be allowed or may be not allowed. So insert appropriate checks into the above functions if necessary. There is a function called strdup available in POSIX systems, but that one allows neither NULL arguments nor does it use the C++ operator new to allocate memory.

Anyway, i've looked with google codesearch for newstrdup functions and found quite a few. Maybe your library is among the results:

Google CodeSearch, newstrdup

你的往事 2024-07-21 12:54:06

他们编写“新”版本的 strdup 一定是有原因的。 所以一定存在它以不同方式处理的极端情况。 就像空字符串可能会返回空字符串一样。

litb 的答案是 strdup 的替代品,但我认为他们这样做是有原因的他们做了什么。

如果你想直接使用strdup,使用一个define来重命名它,而不是编写新的代码。

there has to be a reason that they wrote a "new" version of strdup. So there must be a corner case that it handles differently. like perhaps a null string returns an empty string.

litb's answer is a replacement for strdup, but I would think there is a reason they did what they did.

If you want to use strdup directly, use a define to rename it, rather than write new code.

杯别 2024-07-21 12:54:06

*string = newstrdup("Some string gone here"); 行并没有对 newstrdup 表现出任何奇怪的地方。 如果 string 的类型为 char **,则 newstrdup 仅按预期返回 char *。 据推测,string 已被设置为指向要放置结果的 char * 类型的变量。 否则,代码是通过未初始化的指针写入的。

The line *string = newstrdup("Some string goes here"); is not showing any weirdness to newstrdup. If string has type char ** then newstrdup is just returning char * as expected. Presumably string was already set to point to a variable of type char * in which the result is to be placed. Otherwise the code is writing through an uninitialized pointer..

一影成城 2024-07-21 12:54:06

newstrdup 可能正在创建一个与传递的字符串重复的新字符串; 它返回一个指向字符串的指针(字符串本身就是一个指向字符的指针)。

newstrdup is probably making a new string that is a duplicate of the passed string; it returns a pointer to the string (which is itself a pointier to the characters).

乄_柒ぐ汐 2024-07-21 12:54:06

看起来他编写了一个 strdup() 函数来操作现有的指针,可能会将其重新分配到新的大小,然后填充其内容。 很可能,他这样做是为了在 *string 将频繁更改的循环中重复使用同一指针,同时防止每次后续调用 strdup() 时发生泄漏。

我可能会像 string = redup(&string, "newcontents") 那样实现它..但这只是我。

编辑

这是我的“redup”函数的一个片段,它可能会执行与您发布的类似的操作,只是以不同的方式:

int redup(char **s1, const char *s2)
{
    size_t len, size;

    if (s2 == NULL)
        return -1;

    len = strlen(s2);
    size = len + 1;

    *s1 = realloc(*s1, size);

    if (*s1 == NULL)
        return -1;

    memset(*s1, 0, size);
    memcpy(*s1, s2, len);

    return len;
}

当然,我应该保存 *s1 的副本并恢复它如果 realloc() 失败,但我不需要那么偏执。

It looks like he's written a strdup() function to operate on an existing pointer, probably to re-allocate it to a new size and then fill its contents. Likely, he's doing this to re-use the same pointer in a loop where *string is going to change frequently while preventing a leak on every subsequent call to strdup().

I'd probably implement that like string = redup(&string, "new contents") .. but that's just me.

Edit:

Here's a snip of my 'redup' function which might be doing something similar to what you posted, just in a different way:

int redup(char **s1, const char *s2)
{
    size_t len, size;

    if (s2 == NULL)
        return -1;

    len = strlen(s2);
    size = len + 1;

    *s1 = realloc(*s1, size);

    if (*s1 == NULL)
        return -1;

    memset(*s1, 0, size);
    memcpy(*s1, s2, len);

    return len;
}

Of course, I should probably save a copy of *s1 and restore it if realloc() fails, but I didn't need to get that paranoid.

我最亲爱的 2024-07-21 12:54:06

我认为您需要查看代码中的“string”变量发生了什么,因为 newstrdup() 函数的原型似乎与库 strdup() 版本相同。

代码中是否有 free(*string) 调用?

这看起来是一件奇怪的事情,除非它在内部保留重复字符串的副本并再次返回指向同一字符串的指针。

我又要问为什么?

I think you need to look at what is happening with the "string" variable within the code as the prototype for the newstrdup() function would appear to be identical to the library strdup() version.

Are there any free(*string) calls in the code?

It would appear to be a strange thing do to, unless it's internally keeping a copy of the duplicated string and returning a pointer back to the same string again.

Again, I would ask why?

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