How to get the real and total length of char * (ch

2020-01-27 15:32发布

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");

标签: c++ c arrays char
15条回答
狗以群分
2楼-- · 2020-01-27 16:10

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 use strlen(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.

查看更多
男人必须洒脱
3楼-- · 2020-01-27 16:10

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 using strlen() ? 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:

查看更多
女痞
4楼-- · 2020-01-27 16:10

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.

char a[] = "aaaaa";

int len1 = sizeof(a)/sizeof(char); // length = 6
int len2 = sizeof(a);              // length = 6;

This is the same for both len1 and len2 because this division of 1 does not influence the equation.

The reason why both len1 and len2 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.

char *a = new char[10];
int length = sizeof(a)/sizeof(char);

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.

查看更多
\"骚年 ilove
5楼-- · 2020-01-27 16:11

Strlen command is working for me . You can try following code.

// char *s

unsigned int  strLength=strlen(s);
查看更多
Deceive 欺骗
6楼-- · 2020-01-27 16:15

Given just the pointer, you can't. You'll have to keep hold of the length you passed to new[] or, better, use std::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.

查看更多
再贱就再见
7楼-- · 2020-01-27 16:15

In C++17 (or newer) you can use the std::string_view as a zero-overhead wrapper for string literals.

查看更多
登录 后发表回答