简体   繁体   中英

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. 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. Yes i know about std::string, yes i use that. This isn't my code.

To initialize the string to 0, you could do:

char description[256] = {0};

that will assign 0 to every element of the array.

Just setting the first element to 0 ( '\\0' ) does not erase it's contents. 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' ).

Setting the first element of the char array to \\0 is enough to ensure that 'description' is a properly formatted, actual string. Elements 1 thru 255 can all be garbage, so long as element 0 is 0, description is a zero-length string.

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.

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:

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.

  • No, description[0]=0 is not the same thing as free(description) or 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. 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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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