Why is strdup considered to be evil

Two reasons I can think of: It’s not strictly ANSI C, but rather POSIX. Consequently, some compilers (e.g. MSVC) discourage use (MSVC prefers _strdup), and technically the C standard could define its own strdup with different semantics since str is a reserved prefix. So, there are some potential portability concerns with its use. It hides … Read more

Why do I first have to strcpy() before strcat()?

strcat will look for the null-terminator, interpret that as the end of the string, and append the new text there, overwriting the null-terminator in the process, and writing a new null-terminator at the end of the concatenation. char stuff[100]; // ‘stuff’ is uninitialized Where is the null terminator? stuff is uninitialized, so it might start … Read more

C – split string into an array of strings

Since you’ve already looked into strtok just continue down the same path and split your string using space (‘ ‘) as a delimiter, then use something as realloc to increase the size of the array containing the elements to be passed to execvp. See the below example, but keep in mind that strtok will modify … 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

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

Using a C string gives a warning: “Address of stack memory associated with local variable returned”

Variable char* matches[1]; is declared on the stack, and it will be automatically released when the current block goes out of scope. This means when you return matches, memory reserved for matches will be freed, and your pointer will point to something that you don’t want to. You can solve this in many ways, and … Read more

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