Home > Back-end >  string uppercasing adds junk to the end of char array
string uppercasing adds junk to the end of char array

Time:09-21

I am learning c , I write some code to convert a string to uppercase and display it. I assign a string str with "asdf" and then create a char array pointer and allocate a length same as that of the string.

But after I assign indices of char array with uppercase chars when I try to display char array there are many junk characters appended to the end. Why does this happen as I have only allocated the char array with a size = "length of string" then how does char array have junk chars at the end even after the actual allocated size.

string str{ "asdf" };
char* str_c = new char[str.length()];   
for (int i = 0; i < str.length(); i  ) {
    str_c[i] = toupper(str[i]);     
}
cout << str_c; // displays ASDF²▌r┐│w²A∙

CodePudding user response:

Your char array needs to be one character longer than the length of the string, for the null terminator

string str{ "asdf" };
char* str_c = new char[str.length()   1];   
for (int i = 0; i < str.length(); i  ) {
    str_c[i] = toupper(str[i]);     
}
str_c[str.length()] = '\0';
cout << str_c; // displays ASDF

In C-style strings (char*) the length of the string is not stored in memory. Thus, it must use some other way to determine where the string ends. The way it does that is by assuming that the string is all the bytes until the byte that is equal to zero (so-called null-terminator).

Without explicitly allocating an extra byte for the null terminator, and setting it, your string continues with the garbage that happens to be after the bytes you have allocated until it encounters (accidentally) the nearest 0.

  • Related