null-terminated

Non null-terminated string compiler option for gcc

一世执手 提交于 2019-11-28 03:54:33
问题 Update turns out this is just another case of "c++ is not c blues" What I want const char hex[16] = "0123456789ABCDEF"; the only thing that works char hex[16] = "0123456789ABCDE"; hex[15] = "F"; are there any compiler options or something I can do to make strings not null terminated in the gcc compiler. so that I can make a(n) constant array 回答1: No need for a compiler option, it's already non-NUL terminated. The standard says a NUL should only be added if it can fit, otherwise it would be an

Can a std::string contain embedded nulls?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 22:40:15
For regular C strings, a null character '\0' signifies the end of data. What about std::string , can I have a string with embedded null characters? Yes you can have embedded nulls in your std::string . Example: std::string s; s.push_back('\0'); s.push_back('a'); assert(s.length() == 2); Note: std::string 's c_str() member will always append a null character to the returned char buffer; However, std::string 's data() member may or may not append a null character to the returned char buffer. Be careful of operator+= One thing to look out for is to not use operator+= with a char* on the RHS. It

Copying non null-terminated unsigned char array to std::string

此生再无相见时 提交于 2019-11-27 19:16:36
If the array was null-terminated this would be pretty straight forward: unsigned char u_array[4] = { 'a', 's', 'd', '\0' }; std::string str = reinterpret_cast<char*>(u_array); std::cout << "-> " << str << std::endl; However, I wonder what is the most appropriate way to copy a non null-terminated unsigned char array, like the following: unsigned char u_array[4] = { 'a', 's', 'd', 'f' }; into a std::string . Is there any way to do it without iterating over the unsigned char array ? Thank you all. std::string has a constructor that takes a pair of iterators and unsigned char can be converted (in

strstr() for a string that is NOT null-terminated

陌路散爱 提交于 2019-11-27 17:12:47
问题 How do I do the in-place equivalent of strstr() for a counted string (i.e. not null-terminated) in C? 回答1: If you're afraid of O(m*n) behaviour - basically, you needn't, such cases don't occur naturally - here's a KMP implementation I had lying around which I've modified to take the length of the haystack. Also a wrapper. If you want to do repeated searches, write your own and reuse the borders array. No guarantees for bug-freeness, but it seems to still work. int *kmp_borders(char *needle,

How to get a null terminated string from a C# string?

做~自己de王妃 提交于 2019-11-27 14:42:26
I am communicating with a server who needs null terminated string How can I do this smartly in C#? Thorarin I assume you're implementing some kind of binary protocol, if the strings are null terminated. Are you using a BinaryWriter ? The default BinaryWriter writes strings as length prefixed. You can change that behavior: class MyBinaryWriter : BinaryWriter { private Encoding _encoding = Encoding.Default; public override void Write(string value) { byte[] buffer = _encoding.GetBytes(value); Write(buffer); Write((byte)0); } } Then you can just write any string like this: using (MyBinaryWriter

Why do strings in C need to be null terminated?

我怕爱的太早我们不能终老 提交于 2019-11-27 07:51:33
Just wondering why this is the case. I'm eager to know more about low level languages, and I'm only into the basics of C and this is already confusing me. Do languages like PHP automatically null terminate strings as they are being interpreted and / or parsed? From Joel's excellent article on the topic: Remember the way strings work in C: they consist of a bunch of bytes followed by a null character, which has the value 0. This has two obvious implications: There is no way to know where the string ends (that is, the string length) without moving through it, looking for the null character at

String termination - char c=0 vs char c='\\0'

对着背影说爱祢 提交于 2019-11-27 07:04:06
When terminating a string, it seems to me that logically char c=0 is equivalent to char c='\0' , since the "null" (ASCII 0) byte is 0 , but usually people tend to do '\0' instead. Is this purely out of preference or should it be a better "practice"? What is the preferred choice? EDIT: K&R says : "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0 . http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart Binary Oct Dec Hex Abbr

What happened when we do not include '\0' at the end of string in C?

做~自己de王妃 提交于 2019-11-27 03:42:19
问题 In C, when I initialize my array this way: char full_name[] = { 't', 'o', 'a', 'n' }; and print it with printf("%s", full_name); and run it with valgrind I got error Uninitialised value was create by stack allocation Why do that happen? 回答1: Since %s format specifier expects a null-terminated string, the resulting behavior of your code is undefined. Your program is considered ill-formed, and can produce any output at all, produce no output, crash, and so on. To put this shortly, don't do that

Are char * argv[] arguments in main null terminated?

℡╲_俬逩灬. 提交于 2019-11-27 03:01:41
问题 So I'm wondering if command line parameters are always null terminated? Google seems to say yes, and compiling on GCC indicates this is the case, but can I guarantee this to always be true? int main(int argc, char** argv) { char *p; for(int cnt=1; cnt < argc; ++cnt) { p = argv[cnt]; printf("%d = [%s]\n", cnt, p); } return 0; } $ MyProgram -arg1 -arg2 -arg3 1 = -arg1 2 = -arg2 3 = -arg3 回答1: Yes. The non-null pointers in the argv array point to C strings, which are by definition null

Are all char arrays automatically null-terminated?

假如想象 提交于 2019-11-27 02:21:54
问题 Probably I'm just too dump for googling, but I always thought char arrays get only null terminated by an literal initialization ( char x[]="asdf"; ) and got a bit surprised when I saw that this seems not to be the case. int main() { char x[2]; printf("%d", x[2]); return 0; } Output: 0 Shouldn't an array declared as size=2*char actually get the size of 2 chars? Or am I doing something wrong here? I mean it isn't uncommon to use a char array as a simple char array and not as a string, or is it?