字符串类型转换和 strdup

发布于 2024-12-08 22:23:43 字数 476 浏览 6 评论 0原文

如果输入 const 字符串以某种方式被修改(这会导致 C 编译器警告),处理它的最佳方法是什么 - 将其类型转换为新变量,然后使用它或复制它并使用它,然后释放它。或者有没有其他方法来处理这种情况。请建议。任何帮助将不胜感激。

//类型转换

const char * s1;
char * s2 = (char *)s1;

//重复和免费

const char * s1;
char * s2  = strdup( s1 );
free(s2)

编辑:它是一个C编译器;不是C++。我不确定在类型转换中,s2 是否会是字符串 s1 的新副本,还是指向原始字符串 s1?

感谢您的回答。我还有一个疑问——

const char * c1;
const char * c2 = c1;

上面的作业有效吗?

If an input const string is being modified in some way (which is resulting in C compiler warning), what is the best way to handle it - typecasting it to a new variable and then using it OR duplicating it and using it and then freeing it. Or is there any other way to handle this type of scenario. please suggest. Any help would be appreciated.

//Typecasting

const char * s1;
char * s2 = (char *)s1;

//Duplicate and free

const char * s1;
char * s2  = strdup( s1 );
free(s2)

EDIT: It is a C compiler; not C++. I am not sure whether in typecasting, s2 will be a new copy of string s1 or will it be pointing to the original string s1?

Thanks for the answers. I have one more doubt-

const char * c1;
const char * c2 = c1;

Is the above assignment valid?

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

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

发布评论

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

评论(3

小清晰的声音 2024-12-15 22:23:43

在这里放弃 const 可能会关闭编译器,但会导致运行时失败。复制该字符串并对其进行处理。

丢弃 const 不会复制内存的内容。它只是创建一个指向同一内存的指针,并告诉编译器它可以直接写入该内存。如果内存是只读的,则存在保护故障。更严重的是,您可能会遇到难以调试的正确性问题。不要抛弃 const。

当然,如果您需要修改变量并使这些修改对调用者可见,那么您不应该首先将其设置为 const。另一方面,如果修改是函数私有的,那么复制 const 参数是最好的。

作为一条广泛的规则,如果可能的话,您应该尝试避免强制转换。强制转换是最常见的错误来源之一。

Casting away const here might shut the compiler up but will lead to runtime failures. Make a copy of the string and work on that.

Casting away const does not copy the contents of the memory. It just creates a pointer to the same memory and tells the compiler that it can go right ahead and write to that memory. If the memory is read only you have a protection fault. More seriously you can have correctness problems which are hard to debug. Don't cast away const.

Of course, if you need to modify a variable and have those modifications visible to the caller, then you should not make it const in the first place. On the other hand, if the modifications are meant to be private to the function, then duplication of the const parameter is best.

As a broad rule you should attempt to avoid casts if at all possible. Casts are one of the most frequent sources of errors.

新一帅帅 2024-12-15 22:23:43

如果s1指向的数组是const,那么你不能修改它;这样做会产生未定义的行为。在这种情况下,如果您需要修改它,则必须复制一份。更新:类型转换将为您提供指向同一个数组的指针,该数组仍然是 const,因此您仍然不能修改(即使尝试这样做不再给出编译结果)错误)。

如果您可以保证该数组不是 const,那么您可以通过从指针中删除 const 来修改它。在这种情况下,如果您可以通过首先不向 s1 添加 const 限定符来强制执行该保证,那么就不太容易出错。

If the array that s1 points to is const then you must not modify it; doing so gives undefined behaviour. In that case, if you need to modify it, then you will have to take a copy. UPDATE: Typecasting will give you a pointer to the same array, which is still const, so you still must not modify (even though trying to do so no longer gives a compile error).

If you can guarantee that the array is not const, then you could modify it by casting away the const from the pointer. In that case, it would be less error-prone if you can enforce that guarantee by not adding a const qualifier to s1 in the first place.

画离情绘悲伤 2024-12-15 22:23:43

如果您的输入是 const char*,您绝对应该听取编译器警告并不要管它。

If your input is a const char* you definitely should listen to the compiler warning and leave it alone.

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