How does an std::string or char* hold so many characters and be only 4B?

by Rachel on April 17, 2011

I cant quite figure out how an std::string or char* can hold a seemingly infinite number of characters and yet be only 4B in memory. Logically, 4B can only hold 4 characters, since each ASCII character takes 1 byte to store…

{ 3 comments… read them below or add one }

Ratchetr April 17, 2011 at 11:33 am

A char * is a pointer to a string. A pointer (on your system) takes 4 bytes. Those 4 bytes don’t contain the string. They contain the memory address of where the string is stored.

So, if the value of your char * is 1234, then that means your string, say ‘Hello’ is stored starting at memory address 1234.
If you looked at memory, it would look like this:
1234 ‘H’
1235 ‘e’
1236 ‘l’
1237 ‘l’
1238 ‘o’
1239 ‘’ // Strings in C are always nul terminated.

So…your char * consumes 4 bytes. The string ‘Hello’ consumes 6 bytes. 10 bytes total.

The same logic applies to std::string. string is just a big, user friendly wrapper around char * and all the C string functions.

ETA: What made you believe that std::string is only 4 bytes?
cout << sizeof(std::string); Says 32 on my system. I would be surprised if any system could do it in just 4. Where did you get that number???

John April 17, 2011 at 11:52 am

Well, char* is a pointer, and all pointers are 4 bytes in size, because a pointer is just a memory address. On a 32-bit platform, a pointer is 4 bytes because 32 bits is 4 bytes. That doesn’t represent the whole size of the string; a char* just points to the beginning.

However, I would think that a std::string would be the size of the whole string because it represents the object.

Techwing April 17, 2011 at 12:42 pm

The four bytes contain a pointer to the string itself. The pointer is nothing more than a memory address. On a 32-bit machine (meaning that 32-bit addresses are used by the hardware), the pointer requires 32/8=4 bytes.

This pointer does not actually contain the text of the string. That has to be placed elsewhere in memory, and requires one byte per character (plus an optional delimiter, and sometimes two or more bytes per character if Unicode is being used).

So there’s no infinite capacity. The string of characters does indeed take up memory. But a pointer just points to the place in memory where the string is stored, so the pointer itself requires only four bytes, no matter how long the string that it is pointing to might be.

Leave a Comment

Previous post:

Next post: