Feb 5, 2013 at 10:51pm UTC
Title says it all, does anybody know how?
Last edited on Feb 5, 2013 at 11:05pm UTC
Feb 5, 2013 at 11:17pm UTC
I guess you could do strcpy( myString, "" );
.
Feb 6, 2013 at 1:02am UTC
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':
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 UTC
Feb 6, 2013 at 1:19am UTC
std::fill - The improved C++ alternative to memset.
http://www.cplusplus.com/reference/algorithm/fill/
Last edited on Feb 6, 2013 at 1:20am UTC
Feb 6, 2013 at 5:02am UTC
^ 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 UTC
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 UTC
lol, I made that thing in 5 sec and I didn't bother to check it
Feb 6, 2013 at 11:03pm UTC
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 UTC
Feb 6, 2013 at 11:30pm UTC
Last edited on Feb 6, 2013 at 11:31pm UTC
Feb 7, 2013 at 1:17am UTC
@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:55am UTC
I don't think that that's guaranteed.
Feb 7, 2013 at 2:59am UTC
I'm pretty sure the C++ standard does guarantee that.
Feb 7, 2013 at 3:04am UTC
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 UTC
Feb 7, 2013 at 3:07am UTC
So what's all the talk about there being theoretical compilers with characters being 64 bits?
Feb 7, 2013 at 3:08am UTC
No problem with that, sizeof(char) == sizeof(int) == sizeof(long long) == 1 is valid
Feb 7, 2013 at 3:21am UTC
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 UTC
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 UTC
Ah, I forgot that C++ supported a bits-per-byte different than 8.