简体   繁体   English

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

[英]What is the best way to empty a char string in C?

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? 还是有空而不创建任何新函数或使用任何其他库来清空字符串?

Setting first char to nul is perfectly acceptable. 将第一个字符设置为nul是完全可以接受的。 But if that string was sensitive in terms of security, then you should zero it out with memset . 但是,如果该字符串在安全性方面敏感,则应使用memset将其归零。

Edit: 编辑:

Answer from Matteo Italia made me dig a bit deeper on this subject. Matteo Italia的回答使我对这个问题更深入了。 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. 根据这个文件 (及Matteos答案) memset可能被优化掉,所以是不会从内存中删除敏感信息的最佳选择。 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. 该文档有几个选项,但是它们都不是可移植且可靠的,因此仅出于此类目的,它提出了新的功能标准memset_s 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). 此功能尚不存在,因此我们目前只能使用非便携式( SecureZeroMemory ),非可靠( volatile技巧)或非最佳选项( secure_memset示例)。

There's really no concept of emptying a char string in C. It's simply a pointer that points to some allocated memory. 实际上,没有在C中清空char字符串的概念。它只是指向某些已分配内存的指针。 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. 如果出于某种原因要明确清除字符串的所有内容,请使用其他答案中给出的memset方法。

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'. 如果要“空”它,就意味着在打印时将不打印任何内容,那么可以,只需将第一个字符设置为“ \\ 0”即可。


To conclude, it all depends on what you really want to do. 总而言之,这取决于您真正想做什么。 Why do you want to "empty" the string? 为什么要“清空”字符串?

Use memset instead. 请改用memset 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));

IIRC, you might use memset this way: IIRC,您可以通过以下方式使用memset:

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

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
memset(name, 0, 50); 

要么

bzero(name, 50);

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 eg SecureZeroMemory on Windows). 相反,如果您想从敏感数据中清除字符串,则应使用memset其完全memset (某些操作系统还具有“安全”清零功能,应保证不会被编译器进行优化-参见例如SecureZeroMemory on视窗)。

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. 另一方面,如果要调用的函数只是将要传递的缓冲区用作输出缓冲区,而不考虑其内容,则可以将缓冲区保持原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM