Reputation: 41
string str; str="hello"; str.length(); sizeof(str);
I see that str.length returns the length in bytes why sizeof(str) doesn't return the same?
Is there alternative in c++ to a c command which is strlen(str)? What is the alternative of this coomand in c++?
When I use winsock in the send function I return the length in bytes. What should I use? str.length? Or sizeof(str)? Pr something else? Because I see they produce different results.
Upvotes: 3
Views: 2323
Reputation: 12068
Operator sizeof()
returns size of given type or object in bytes. 'Type version' is quite simple to understand, bu with 'Object version' you need to rember one thing:
sizeof()
looks only on type definition and deduces total size from size and number of its members (in general, polymorphic and multiple inherited types may have additional 'hidden' members).
In other words, let's assume we have:
struct A
{
int* p1;
char* p2;
};
As you can probably suspect, sizeof(A)
will return 8 (as pointer is 4-byte type on most 32-bit systems). But, when you do something like this:
A a_1;
a_1.p1 = new int[64];
sizeof(a_1)
will still return 8. That's because memory allocated by new and pointed by A's member, does not 'belong' to this object.
And that is why sizeof(str)
and str.length()
give different results. std::string
allocates memory for chars on the heap (dynamically, via malloc()
), so it doesn't change string's size.
So, if you want to send string via network, proper size is str.len()
and data pointer can be retrieved by calling str.c_str()
.
I didn't understant part with "strlen(str)
equivalent". In C++ there is also strlen()
function, with the same prototype, working exactly in the same way. It simply requires const char*
, so you cannot use it for std::string
(but you can do strlen(str.c_str())
, as std::string
's internal string is guaranteed to be null-terminated). For std::string
use .length()
as you already did.
Upvotes: 0
Reputation: 49832
sizeof returns the size of the data structure, not the size of the data in contains.
length() returns the length of the string that str contains, and is the function you want
It might seem confusing because sizeof(char[30]) is 30, but that is because the size of the data structure is 30, and will remain 30 no matter what you put in it
The string is actually an extremely complicated structure, but suppose it was a simple class with a pointer and a length
class string
{
char *data;
int length;
};
then sizeof(string) would return:
The size of a char * pointer, possibly but not necessarily 4
plus the size of an int, possibly but not necessarily 4
So you might get a value of 8. What the value of data or length is has no effect on the size of the structure.
Upvotes: 4
Reputation: 575
sizeof()
is not really meant to be used on a string class. The string class doesn't store ONLY the string data; there would be no difference between its data and a C-style string; it has other stuff in it as well, which throws off sizeof(). To get the actual length of the characters in the string, use str.length()
.
Don't use the C strlen() on a C++ string object. Don't use sizeof() either. Use .length().
Upvotes: 2
Reputation: 6904
std::string
in C++ is instantiated as a pointer to a string object, since a string may have varying length. What sizeof()
is returning is the size of the pointer to the string object (which on a 32 bit machine will probably be 4
)
Upvotes: 0