I have to write a piece of code and deploy it in a system with limited memory space and was thinking about char arrays vs string . I wrote the following piece of code to get a better idea.
string str="abcde";
char carray[6]={'a','b','c','d','e','\0'};
cout <<"size of char array:"<< sizeof(carray) << endl ;
cout <<"size of string:"<< sizeof(str) << endl ;
The output I get is
size of char array:6
size of string:28
So, I have 2 questions
- Can I conclude that using the string will be a foolish idea given that it is taking much more space, given that memory space is tight and the max chars in my array wont be greater than 10?
- Why the hell is size of string showing as 28? on what basis?