How to clear data from a char array?

Feb 5, 2013 at 10:51pm
Title says it all, does anybody know how?
Last edited on Feb 5, 2013 at 11:05pm
Feb 5, 2013 at 11:17pm
I guess you could do strcpy( myString, "" );.


Feb 6, 2013 at 1:02am
What exactly do you mean by "clear data"?

Do you mean you want it to be an empty string? If so, you can use iHutch's method, or else set the first element to be '\0':

 
myString[0] = '\0';


Or do you mean that you want to set every element of the array to '\0'?

Or do you mean that you want to destroy a dynamically allocated array of characters so that the memory is released?

Or do you mean something else?
Last edited on Feb 6, 2013 at 1:02am
Feb 6, 2013 at 1:19am
closed account (S6k9GNh0)
std::fill - The improved C++ alternative to memset.

http://www.cplusplus.com/reference/algorithm/fill/
Last edited on Feb 6, 2013 at 1:20am
Feb 6, 2013 at 3:04am
do

1
2
3
4
for(int erase = 0;;erase++)
{
      array[erase] = '\n';
}


something like that, try it
Feb 6, 2013 at 5:02am
^ that will put you into an infinite loop and crash your program. You might want to specify a condition like erase < int(array.length());
Feb 6, 2013 at 6:04am
closed account (S6k9GNh0)
Or you could use built-in compiler-optimized functions to do your job like I specified. But what do I know...
Feb 6, 2013 at 9:08pm
lol, I made that thing in 5 sec and I didn't bother to check it
Feb 6, 2013 at 11:03pm
use this:

1
2
3
4
5
char str[] = "Hello";

for (int i = 0; i < sizeof(str); i++) {
        str[i] = '\0';
}
Last edited on Feb 6, 2013 at 11:05pm
Feb 6, 2013 at 11:30pm
Please take personally: http://www.cplusplus.com/forum/beginner/1988/5/#msg20048

computerquip posted a perfectly valid solution that works in all cases. Please don't post additional solutions, especially if they don't work in all cases.
Last edited on Feb 6, 2013 at 11:31pm
Feb 7, 2013 at 1:17am
@Sadegh2007

sizeof (str) will not do exactly what you think it does. What it does it that is returns the size in bytes of each character in the string and adds them together. So to use sizeof, you have to divide by the size of one char to get you the number of characters in the string
Feb 7, 2013 at 2:06am
iirc sizeof(char) == 1
Feb 7, 2013 at 2:55am
I don't think that that's guaranteed.
Feb 7, 2013 at 2:59am
I'm pretty sure the C++ standard does guarantee that.
Feb 7, 2013 at 3:04am
That's in the very definition of the sizeof operator
ISO wrote:
sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.
Last edited on Feb 7, 2013 at 3:04am
Feb 7, 2013 at 3:07am
So what's all the talk about there being theoretical compilers with characters being 64 bits?
Feb 7, 2013 at 3:08am
No problem with that, sizeof(char) == sizeof(int) == sizeof(long long) == 1 is valid
Feb 7, 2013 at 3:21am
Wait, what? Why would sizeof(some_64_bit_type) == 1? I thought it was the number of bytes, not the number of native word sizes?
Feb 7, 2013 at 3:53am
sizeof returns the number of bytes. C++ is valid for a platform where there are 64 bits in a byte (and one byte in a long long and one byte in a char). It's also valid for a platform with one's complement arithmetic and other impractical setups.

(to keep it less off-topic, I support std::fill)
Feb 7, 2013 at 4:04am
Ah, I forgot that C++ supported a bits-per-byte different than 8.
Topic archived. No new replies allowed.