简体   繁体   English

擦除Char []

[英]Erasing a Char[]

Okay i am working on someone elses code. 好吧,我正在研究别人的代码。 They do alot of this: 他们做了很多这样的事情:

char description[256];
description[0]=0;

I know this would put a \\0 in the first spot of the character array. 我知道这会在字符数组的第一个位置放置\\ 0。 But is this even a safe way to erase a string? 但这甚至是一种擦除字符串的安全方法吗?

Also visual studio keeps reporting memory leaks, and i've pretty much tied this done to the strings that are used. 视觉工作室也会报告内存泄漏,我几乎把这个完成与使用的字符串联系起来。

Ps. PS。 Yes i know about std::string, yes i use that. 是的我知道std :: string,是的,我使用它。 This isn't my code. 这不是我的代码。

To initialize the string to 0, you could do: 要将字符串初始化为0,您可以执行以下操作:

char description[256] = {0};

that will assign 0 to every element of the array. 这将为数组的每个元素分配0。

Just setting the first element to 0 ( '\\0' ) does not erase it's contents. 将第一个元素设置为0'\\0' )不会删除它的内容。 It doesn't even guarantee the entire string is set to the null character. 它甚至不保证整个字符串被设置为空字符。

As stated by others, you can't "erase" statically-created objects until the function closes, when it gets abandoned. 正如其他人所说,在函数关闭之前,当它被放弃时,你不能“擦除”静态创建的对象。 Technically, it's not erased when the function is abandoned either - the stack pointer is merely changed. 从技术上讲,当函数被放弃时它也不会被删除 - 堆栈指针只是被改变了。 If you're paranoid about the data being erased, you should iterate through the array, setting each entry to 0 ( '\\0' ). 如果你对被删除的数据感到偏执,你应该遍历数组,将每个条目设置为0'\\0' )。

Setting the first element of the char array to \\0 is enough to ensure that 'description' is a properly formatted, actual string. 将char数组的第一个元素设置为\\ 0就足以确保'description'是一个格式正确的实际字符串。 Elements 1 thru 255 can all be garbage, so long as element 0 is 0, description is a zero-length string. 元素1到255都可以是垃圾,只要元素0为0,描述就是零长度字符串。

You dont have to worry about memory leaks in the code posted above, because the array is allocated on the stack. 你不必担心上面发布的代码中的内存泄漏,因为数组是在堆栈上分配的。 Once it falls off the stack (goes out of scope), the char array is deallocated. 一旦它从堆栈中掉落(超出范围),char数组就会被释放。

This string is allocated on the stack, so there's no way to free the memory it uses until the function that it's called in returns (when it will happen automatically). 这个字符串是在堆栈上分配的,所以没有办法释放它使用的内存,直到它调用的函数返回(当它自动发生时)。 Unless you're calling this function recursively*, there's no way this will end up being a memory leak, because once the function returns the space is used for future stack frames. 除非你以递归方式调用此函数*,否则这将无法成为内存泄漏,因为一旦函数返回,该空间将用于将来的堆栈帧。 And if you're concerned about security, you should just loop through and zero out the elements of the string. 如果你担心安全性,你应该循环并将字符串的元素清零。

If you want a free()-able memory block, you could do the following and allocate the array on the heap: 如果你想要一个free() - 内存块,你可以执行以下操作并在堆上分配数组:

char *str = malloc(256*sizeof(char)); // str now is a pointer to a 256-char array
...
// some code here
...
free(str); // free the memory

*this is not an acutal memory leak, but some people say "memory leak" when they mean "run out of memory". *这不是一个实际的内存泄漏,但有些人说“内存泄漏”,当他们的意思是“内存不足”。 In any case, stack space is much more limited than heap space, so you have to watch the size of memory blocks you use there. 在任何情况下,堆栈空间比堆空间更受限制,因此您必须观察在那里使用的内存块的大小。

To clarify the good answers given so far: 澄清到目前为止给出的好答案:

  • Yes, description[0]=0 clears the string from strxxx functions POW: strlen(description) == 0, strcmp(description, "") == 0 and std::string(description) == "" are all true. 是的,description [0] = 0从strxxx函数中清除字符串POW:strlen(description)== 0,strcmp(description,“”)== 0和std :: string(description)==“”都是真的。

  • No, description[0]=0 is not the same thing as free(description) or memset(description, 0, sizeof description). 不,description [0] = 0与free(description)或memset(description,0,sizeof description)不同。 But you already knew that. 但你已经知道了。

  • The piece of code you cite cannot possibly result in memory leak. 你引用的那段代码不可能导致内存泄漏。 The memory is not allocated on the heap, and memory leaks are a heap thing. 内存没有在堆上分配,内存泄漏是一堆事情。

\\0放在字符串的第一个元素中是一种清除字符串的安全方法,但这与删除字符串不同,并且不会阻止内存泄漏。

If it's a char[] string, and the only operations performed on it are string functions, it's fine. 如果它是一个char []字符串,并且对它执行的唯一操作是字符串函数,那很好。 Of course, it's not good enough for protected data. 当然,它对受保护的数据来说还不够好。

As for memory leaks, it might be worth changing to the safe versions of the string functions, but you can't leak static or stack-based strings, so it's probably somewhere your string is passed out. 至于内存泄漏,可能值得更改为字符串函数的安全版本,但是你不能泄漏静态或基于堆栈的字符串,所以它可能是你的字符串被传递出去的地方。

For clarity, I'd change it to '\\0'. 为清楚起见,我将其更改为'\\ 0'。

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

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