String construct got space for the data by doing
new char[strlen(cp)+1];
Since there are only strlen(cp)characters in the string, what is the extra byte for?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For the special
'\0'char which indicates end of string.(Remember, C-style strings are null-terminated arrays).
Additional helpful notes:
strlendoes not count the'\0'(That’s why you need this extra byte).strcpydoes copy the'\0'.char str[7] = "String";– Adds'\0'by itself.char str[] = {'S','t','r','i','n','g'}– Does not add'\0'.char str[7] = {'S','t','r','i','n','g'}– Will add'\0'.