C 中清空字符串的最佳方法是什么?

发布于 2024-12-20 10:16:36 字数 218 浏览 0 评论 0原文

您好,我有一个字符字符串

name[50] = "I loveprogramming"

发生的情况是,我想在调用另一个函数之前清空该字符串,以便我可以在同一个数组中存储一些

内容这个工作?

name[0] = '\0';

或者是否可以在不创建任何新函数或使用任何其他库的情况下清空字符串?

Hi I have a char string

name[50] = "I love programming"

what happen is that I want to empty this string before I call my another function so that I can store something in the same array

will this work?

name[0] = '\0';

or is there anyway to empty the string without creating any new function or use any other library?

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

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

发布评论

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

评论(7

你穿错了嫁妆 2024-12-27 10:16:36

将第一个字符设置为 nul 是完全可以接受的。但如果该字符串在安全性方面很敏感,那么您应该使用 memset 将其清零。

编辑:

Matteo Italia 的回答让我对这个主题有了更深入的了解。根据 本文档 (和 Matteos 的回答) memset 可以被优化掉,因此不是从内存中删除敏感信息的最佳选择。该文档有几个选项,但没有一个是可移植和可靠的,因此它专门为此目的提出了新的函数标准memset_s。此功能尚不存在,因此我们目前陷入了不可移植(SecureZeroMemory)、不可靠(易失性技巧)或非最佳选项(<代码> secure_memset 示例)。

Setting first char to nul is perfectly acceptable. But if that string was sensitive in terms of security, then you should zero it out with memset.

Edit:

Answer from Matteo Italia made me dig a bit deeper on this subject. According to this document (and Matteos answer) memset could be optimized away, and so is not the best option to remove sensitive information from memory. The document has several options, but none of them is portable and reliable, so it proposes new function standard memset_s just for such purposes. This function does not exist yet, so we're currently stuck with non-portable (SecureZeroMemory), non-reliable (volatile trick), or non-optimal options (secure_memset example).

半城柳色半声笛 2024-12-27 10:16:36

使用 memset 代替。这只会使缓冲区无效,但当变量超出范围时,分配的内存将如何从堆栈中释放。

memset (name,'\0',sizeof(name));

Use memset instead. This would just nullify the buffer but the memory allocated would any how gets deallocated from stack when the variable goes out of scope.

memset (name,'\0',sizeof(name));
生死何惧 2024-12-27 10:16:36

C 中确实没有清空 char 字符串的概念。它只是一个指向某些已分配内存的指针。您可以以任何您希望的方式重用该内存,而无需先“清空”它。所以,最简单的清空方法就是“不要”。

如果您出于某种原因想显式清除字符串的所有内容,请使用其他答案中给出的 memset 方法。

如果您想“清空”它,即在打印时不会打印任何内容,那么可以,只需将第一个字符设置为“\0”即可。


总而言之,这完全取决于您真正想要做什么。 为什么你想“清空”字符串?

There's really no concept of emptying a char string in C. It's simply a pointer that points to some allocated memory. You can reuse that memory in any way you wish, without "emptying" it first. So, the easiest way to empty it is "just don't".

If you want to explicitly clear all contents of the string for some reason, use the memset approach given in other answers.

If you want to "empty" it in the sense that when it's printed, nothing will be printed, then yes, just set the first char to `\0'.


To conclude, it all depends on what you really want to do. Why do you want to "empty" the string?

再可℃爱ぅ一点好了 2024-12-27 10:16:36

IIRC,你可以这样使用 memset:

char * myString;
...
size_t len = strlen(myString)
memset (myString, 0,len);

IIRC, you might use memset this way:

char * myString;
...
size_t len = strlen(myString)
memset (myString, 0,len);
忆梦 2024-12-27 10:16:36

从技术上讲,这是正确的,例如:

char array[10] = "hello";
printf("%d\r\n", strlen(array)); // prints 5
array[0] = '\0';
printf("%d\r\n", strlen(array)); // prints 0

Tehnically it is correct, for example:

char array[10] = "hello";
printf("%d\r\n", strlen(array)); // prints 5
array[0] = '\0';
printf("%d\r\n", strlen(array)); // prints 0
超可爱的懒熊 2024-12-27 10:16:36
memset(name, 0, 50); 

或者

bzero(name, 50);
memset(name, 0, 50); 

or

bzero(name, 50);
梦一生花开无言 2024-12-27 10:16:36

这取决于您想要获得的效果。如果您只想将其长度归零,您可以这样做,正如您所说:

*name='\0';

如果相反,您想清除字符串中的敏感数据,则应该使用 memset 将其完全归零(某些操作系统也有一个“安全”的归零函数,应该保证不会被编译器优化掉 - 参见例如 SecureZeroMemory(Windows 上)。

另一方面,如果您调用的函数仅使用您作为输出缓冲区传递的缓冲区,而不考虑其内容,则可以将缓冲区保持原样。

It depends from the effect you want to obtain. If you just want to zero its length you can do, as you said:

*name='\0';

If, instead, you want to clean your string from sensitive data, you should zero it completely with memset (some operating systems also have a "secure" zeroing function that should be guaranteed not to be optimized away by the compiler - see e.g. SecureZeroMemory on Windows).

On the other hand, if the function you are calling just uses the buffer you are passing as an output buffer disregarding its content, you may just leave the buffer as it is.

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