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");
You can implement your own
new
anddelete
functions, as well as an additionalget-size
function:Alternatively, you can override the
new
anddelete
operators in a similar manner.You can't. Not with 100% accuracy, anyway. The pointer has no length/size but its own. All it does is point to a particular place in memory that holds a char. If that char is part of a string, then you can use
strlen
to determine what chars follow the one currently being pointed to, but that doesn't mean the array in your case is that big.Basically:
A pointer is not an array, so it doesn't need to know what the size of the array is. A pointer can point to a single value, so a pointer can exist without there even being an array. It doesn't even care where the memory it points to is situated (Read only, heap or stack... doesn't matter). A pointer doesn't have a length other than itself. A pointer just is...
Consider this:
A pointer can be a single char, as well as the beginning, end or middle of an array...
Think of chars as structs. You sometimes allocate a single struct on the heap. That, too, creates a pointer without an array.
Using only a pointer, to determine how big an array it is pointing to is impossible. The closest you can get to it is using
calloc
and counting the number of consecutive \0 chars you can find through the pointer. Of course, that doesn't work once you've assigned/reassigned stuff to that array's keys and it also fails if the memory just outside of the array happens to hold\0
, too. So using this method is unreliable, dangerous and just generally silly. Don't. Do. It.Another analogy:
Think of a pointer as a road sign, it points to Town X. The sign doesn't know what that town looks like, and it doesn't know or care (or can care) who lives there. It's job is to tell you where to find Town X. It can only tell you how far that town is, but not how big it is. That information is deemed irrelevant for road-signs. That's something that you can only find out by looking at the town itself, not at the road-signs that are pointing you in its direction
So, using a pointer the only thing you can do is:
But this, of course, only works if the array/string is \0-terminated.
As an aside:
is actually assigning
size_t
(the return type ofsizeof
) to anint
, best write:Since
size_t
is an unsigned type, ifsizeof
returns bigger values, the value oflength
might be something you didn't expect...when new allocates an array, depending on the compiler (i use gnu c++), the word in front of the array contains information about the number of bytes allocated.
The test code:
on my machine dumps out:
I would not recommend this solution (vector is better), but if you are really desperate, you could find a relationship and be able to conclude the number of bytes allocated from the heap.