new char[2] -> strlen == 14 ???

So it's quite a straight forward questioin really?

1
2
3
char* msg1 = new char[2];
len = strlen(msg1);
cout<<len;

output: 14

What? shouldn't it be 2 since Ive only declared an array of lenght 2?
Last edited on
are you trying to find the dynamic array size or the string length?
-- I tried the code and it gave me an output of 0.
http://www.cplusplus.com/reference/clibrary/cstring/strlen/

strlen returns the number of characters before the terminating null character.

The terminating null character also takes up space, so keep in mind that an array 100 big will only store 99 characters of text (indicies 0-98), the 100th being the terminating null character (at index 99).
Put another way (m'learned chum Palm Tree is bang on, so this is just the same again in different words), strlen tells you the number of bytes from where the pointer points to the next zero value. Why do you think there is a zero value two bytes along from the pointer msg1?
Topic archived. No new replies allowed.