If you need space for strings, consider using std::string instead.
Knowing and using std::* classes is
not a replacement for actually understanding how the language works, and in particular how memory allocation works, particularly with a relatively low-level language such as C++. When he has a good understanding of the fundamentals, a basic understanding of the std::* classes and knowledge that he should use them should come along with it.
To the OP:
The reason that
strlen is not returning the size of your array is simply due to the fact that that is not the purpose of
strlen.
strlen returns the length of a C String, that is, a null-terminated character array.
You are allocationg 4 bytes of memory. However, that memory is coming out of the heap, and it is likely that there is "valid" memory after it (valid in that it won't crash, but undefined as per C++).
Since it is not initialized, it just has random junk. You are seeing strlen return 16. This is why:
| YOUR ARRAY |
[rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][rnd][0]
rnd is any random non-zero value.
strlen has no knowledge of your array or its size, and only has knowledge of the pointer and that it must seek the null terminator. The fact that you have consistently gotten 16 is coincidental.
It could very well return this:
| YOUR ARRAY |
[rnd][rnd][0][rnd]
In which case, strlen would return 3.
The reason it could CRASH is that it could have allocated your memory at the edge of a page allocation, for instance. When you go outside the buffer, you are now reading unmapped memory, which is a page fault. There are other reasons too, simply put - don't read outside of memory you are aware of.