Erasing a Char

Okay i am working on someone elses code. They do alot of this:

char description[256];

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.

Need Your Help

Linux “install” command for wildcard installation

linux install

Is there a way to use "install" for installing multiple files at once using a "wildcard" pattern (and still have "install" create the leading directory hierarchy)?

Set Focus on DataGridViewCombobox while pressing arrow key up and down

c# winforms datagridviewcombobox

I have a datagridview in which there are three columns .actually this is a dataprocessing application thats why i want use of keyboard(minimal use of mouse)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.