What is the string type in c++?
Assuming you mean std::string, it is a RAII container for dynamic character string with contiguous storage. It is the canonical type for an abstract "string" type used to represent text in C++.
sizeof(string) and I found it 24. when I tried to make an array of strings I found the compiler also multiplies the size of the array by 24.
This is how arrays work. The elements are sizeof apart from each other. If the distance was less, the elements would overlap each other.
Is that the real size of a string?
On your system, with that specific standard library implementation, yes. sizeof does not lie (in standard C++).
I thought it is only 4
Evidently, you thought wrong.
if it really were 24 bytes how to optimize it ?
You can write a string type of your own. It's going to be a lot of work, and I suspect that achieving the 4 bytes would be challenging on a 64 bit system. Even if you do achieve it, I suspect that the loss in speed would not be acceptable for the benefit of gaining the bytes.
Your own string type won't be std::string though. You can "optimise" that only by writing your own standard library. It's going to be a lot more work.