Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between strlen(str.c_str()) and str.length() for std::string

Tags:

c++

string

stl

As an implicit understanding, I always thought that every implementation of std::string necessarily must satisfy strlen(str.c_str()) == str.length() for every string str.

What does the C++ standard say about this? (Does it?)

Background: At least the implementations shipped with Visual C++ and gcc do not have this property. Consider this example (see here for a live example):

// Output:
// string says its length is: 13
// strlen says: 5
#include <iostream>
#include <cstring>
#include <string>

int main() {
  std::string str = "Hello, world!";
  str[5] = 0;
  std::cout << "string says its length is: " << str.length() << std::endl;
  std::cout << "strlen says: " << strlen(str.c_str()) << std::endl;
  return 0;
}

Of course, the writing operation without str noticing is causing "the problem". But that's not my question. I want to know what the standard has to say about this behavior.

like image 412
Xlea Avatar asked Mar 27 '15 13:03

Xlea


People also ask

What is the difference between strlen and length?

Evaluation Size: sizeof() is a compile-time expression that gives the size of a type or a variable's type. It doesn't care about the value of the variable. strlen() on the other hand gives the length of a C-style NULL-terminated string and size() also returns the length of the specified string.

What is the purpose of the c_str () member function of std::string?

The c_str() method converts a string to an array of characters with a null character at the end. The function takes in no parameters and returns a pointer to this character array (also called a c-string).

What is the difference between std::string and string?

std::string is the string class from the standard C++ library. String is some other string class from some other library. It's hard to say from which library, because there are many different libraries that have their own class called String.

How do you compute the length of the string str C++?

The C++ String class has length() and size() function. These can be used to get the length of a string type object. To get the length of the traditional C like strings, we can use the strlen() function. That is present under the cstring header file.


1 Answers

Your understanding is incorrect. Sort of.

std::string may contain chars with the value '\0'; when you extract a C-string, you have no way of knowing how long it is other than to scan for \0s, which by necessity cannot account for "binary data".

This is a limitation of strlen, and one that std::string "fixes" by actually remembering this metadata as a count of chars that it knows are encapsulated.

The standard doesn't really need to "say" anything about it, except that std::string::length gives you the string length, no matter what the value of the chars you inserted into the string, and that is it not prohibited to insert a '\0'. By contrast, strlen is defined to tell you how many chars exist up to the next \0, which is a fundamentally different definition.

There is no explicit wording about this, because there does not need to be. If there were an exception to the very simple rules ("there is a string; it has chars; it can tell you how many chars it has") then that would be stated explicitly… and it's not.

like image 126
Lightness Races in Orbit Avatar answered Oct 16 '22 16:10

Lightness Races in Orbit