Is std::string always null-terminated in C++11? [duplicate]

Yes, per [string.accessors] p1, std::basic_string::c_str(): Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()]. Complexity: constant time. Requires: The program shall not alter any of the values stored in the character array. This means that given a string s, the pointer returned by s.c_str() must be the same … Read more

Why null-terminated strings? Or: null-terminated vs. characters + length storage

From Joel’s Back to Basics: Why do C strings work this way? It’s because the PDP-7 microprocessor, on which UNIX and the C programming language were invented, had an ASCIZ string type. ASCIZ meant “ASCII with a Z (zero) at the end.” Is this the only way to store strings? No, in fact, it’s one … Read more

What is a null-terminated string?

A “string” is really just an array of chars; a null-terminated string is one where a null character ‘\0’ marks the end of the string (not necessarily the end of the array). All strings in code (delimited by double quotes “”) are automatically null-terminated by the compiler. So for example, “hi” is the same as … Read more

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

Yes. The non-null pointers in the argv array point to C strings, which are by definition null terminated. The C Language Standard simply states that the array members “shall contain pointers to strings” (C99 §5.1.2.2.1/2). A string is “a contiguous sequence of characters terminated by and including the first null character” (C99 §7.1.1/1), that is, … Read more

Can a std::string contain embedded nulls?

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 … Read more

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

std::string has a constructor that takes a pair of iterators and unsigned char can be converted (in an implementation defined manner) to char so this works. There is no need for a reinterpret_cast. unsigned char u_array[4] = { ‘a’, ‘s’, ‘d’, ‘f’ }; #include <string> #include <iostream> #include <ostream> int main() { std::string str( u_array, … Read more

String termination – char c=0 vs char c=’\0′

http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart Binary Oct Dec Hex Abbr Unicode Control char C Escape code Name 0000000 000 0 00 NUL ␀ ^@ \0 Null character There’s no difference, but the more idiomatic one is ‘\0’. Putting it down as char c = 0; could mean that you intend to use it as a number (e.g. a counter). … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)