views:

581

answers:

6
+2  Q: 

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.

+4  A: 

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.

Kyle Cronin
It won't be a memory leak even when called recursively (there may be a bug in the recursion that causes you to run out of memory, but I wouldn't characterize that as a "leak", per se).
Emerick Rogul
True, but sometimes people say "memory leak" when they mean "I ran out of memory"
Kyle Cronin
Potentially this array could be inside a class, if the class is leaked then the whole array is leaked. Depending on what tools the OP is using, things may be confusing.
coryan
and yes, OP knows about std::string, so might have heard of malloc.
Mark
+3  A: 

Putting \0 in the first element of a string is a safe way to clear the string, but that is not the same as deleting the string and will not prevent memory leaks.

finnw
+1  A: 

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'.

Mark
I thought there was no guarantee that the NUL character ('\0') would have a zero value. More than clarity is gained by using '\0', you make your code portable to all those non-ASCII platforms out there. Which you may or may not care about.
coryan
I'm pretty sure that while NUL is ASCII-specific, and null can be anything as long as it's always that, '\0' has to be zero.
Mark
The standard does guarantee that the null terminator used to indicate the end of a string is integral 0.
coppro
+8  A: 

To initialize the string to 0, you could do this:

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).

warren
+8  A: 

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.

John Dibling
+4  A: 

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.

Arkadiy