For a char []
, I can easily get its length by:
char a[] = "aaaaa";
int length = sizeof(a)/sizeof(char); // length=6
However, I cannot do like this to get the length of a char *
by:
char *a = new char[10];
int length = sizeof(a)/sizeof(char);
because, I know, a
here is a pointer, such that length
here will be always be 4
(or something other in different systems).
My question is that how can I get the length of a char *
afterwards? I know someone may challenge me that you already know its 10
because you just created it. I want to know this because this step of getting its length may come long long way from its creation and I don't want to come long long way back to check this number. Moreover, I also want to know its real length.
To be more specific
- how can I get its real
length=5
? - how can I get its total
length=10
?
for the following example:
char *a = new char[10];
strcpy(a, "hello");
There are only two ways:
If the memory pointer to by your
char *
represents a C string (that is, it contains characters that have a 0-byte to mark its end), you can usestrlen(a)
.Otherwise, you need to store the length somewhere. Actually, the pointer only points to one
char
. But we can treat it as if it points to the first element of an array. Since the "length" of that array isn't known you need to store that information somewhere.This may sound Evil™ and I haven't tested it, but how about initializing all values in an array at allocation to
'\0'
and then usingstrlen()
? This would give you your so-called real value since it would stop counting at the first'\0'
it encounters.Well, now that I think about it though, please don't Ever™ do this. Unless, you want to land in a pile of dirty memory.
Also, for the allocated memory or the total memory you may use the following functions if your environment provides them:
So the thing with the sizeof operator is that it returns you the amount of storage needed, in bytes, to store the operand.
The amount of storage needed to store a char is always 1 byte. So the
sizeof(char)
will always return 1.This is the same for both
len1
andlen2
because this division of 1 does not influence the equation.The reason why both
len1
andlen2
carry the value 6 has to do with the string termination char'\0'
. Which is also a char which adds another char to the length. Therefore your length is going to be 6 instead of the 5 you were expecting.You already mentioned that the length turns out to be 4 here, which is correct. Again, the sizeof operator returns the storage amount for the operand and in your case it is a pointer
a
. A pointer requires 4 bytes of storage and therefore the length is 4 in this case. Since you probably compile it to a 32-bit binary. If you'd created a 64-bit binary the outcome would be 8.This explanation might be here already be here. Just want to share my two cents.
Strlen command is working for me . You can try following code.
// char *s
Given just the pointer, you can't. You'll have to keep hold of the length you passed to
new[]
or, better, usestd::vector
to both keep track of the length, and release the memory when you've finished with it.Note: this answer only addresses C++, not C.
In
C++17
(or newer) you can use thestd::string_view
as a zero-overhead wrapper for string literals.