Hi all,

I created a string object and printed its size. Its always priniting output as 16. Why is it like that?
Can anyone give me idea about how string class is implemented.
I am using VC

string input;
cin>>input;
cout<<sizeof(input)<<input<<'\n';

Dani AI

Generated

observed that sizeof on a std::string prints 16. As explained, that 16 is the size of the string object itself (its metadata and any in-object storage), not the number of characters it currently holds. As pointed out, use the string member functions to learn about the text: size()/length() give the number of characters, and capacity() reports how many characters the string can hold without reallocating.

A few practical points that help explain the constant 16 value:

  • sizeof returns the compile-time size of the type layout used by your standard library implementation and ABI. That layout commonly contains pointers/indices, size/capacity fields, and sometimes a small internal buffer. On 32-bit builds these members often sum to 16 bytes; on 64-bit builds the size is usually larger. This is implementation-defined and can change between compiler/library versions.
  • Many modern libraries use a short-string optimization (SSO): a small character buffer lives inside the std::string object so short contents avoid heap allocation. SSO increases the object size but improves performance for small strings.

Use these members and a quick experiment to see the differences between object size and dynamic storage:

std::string s = "hello";
std::cout << "chars: " << s.size()
          << ", capacity: " << s.capacity()
          << ", sizeof(string): " << sizeof(std::string) << '\n';
s.reserve(200);
std::cout << "after reserve capacity: " << s.capacity() << '\n';

Notes and cautions: strlen(s.c_str()) will work but is redundant and O(n); prefer s.size()/s.length() which are O(1). sizeof will never reflect heap allocations; to measure real memory usage (including heap) use a memory profiler or a custom allocator that records allocations. For authoritative details on the container interface and behavior see the C++ reference on basic_string (cppreference basic_string) and the MSVC documentation for the basic string class (MSVC basic_string).

Recommended Answers

All 5 Replies

>Its always priniting output as 16. Why is it like that?
Because that's the actual size of the object. The implementation probably looks something like this:

template <class T, class Traits = char_traits<T>, class Alloc = allocator<T> >
class basic_string {
public:
  // Lots and lots of interface stuff
private:
  size_type size;     // Probably a 32-bit type
  size_type capacity; // Probably a 32-bit type
  _buf_type buffer;
};

Where _buftype is likely defined as something along these lines:

struct _buf_type {
  T *base; // Beginning of buffer
  T *mark; // Helper pointer within buffer
};

The actual size of the string data itself is dynamic, so sizeof won't tell you how much memory has been allocated to the string. You can find that with the capacity member function.

Thanks Narue,

Is there any function to calculate the length of string object. From ur explanation I guess strlen wont work.

Yes, you find the manual / help pages which describe the string class.
This will detail the public interface to the class.

Say perhaps

string foo;
foo.length();

Thanks Salem.

Both the length and size member functions will give you the length of the actual string. Why are there two functions that do the same thing? That's the effect of taking an existing (pre-standard) class and forcing it to fit within the new standard definition of a container class. There are lots of ways the string class is poorly designed, and this is one of them. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.