tags:

views:

168

answers:

8
#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
        {
            cout << "size of String " << sizeof(string );

            system("PAUSE");
            return EXIT_SUCCESS;
        }

Output:

size of String = 4

Does that mean that, since sizeof(char) = 1 Byte (0 to 255), string can only hold 4 characters?

A: 

No, it means that the sizeof the class string is 4.

It does not mean that a string can be contained in 4 bytes of memory. Not at all. But you have to difference between dynamic memory, used to contain the size characters a string can be made of, and the memory occupied by the address of the first of those characters

Try to see it like this:

contents  --------> |h|e|l|l|o| |w|o|r|ld|\0|

sizeof 4 refers to the memory occupied by contents. What it contents? Just a pointer to (the address of ) the first character in the char array.

How many characters does a string can contain ? Ideally, a character per byte available in memory.

How many characters does a string actually have? Well, theres a member function called size() that will tell you just that

size_type size() const

See more on the SGI page !

Tom
What does "sizeof the class String is 4" actually mean?
Kevin
It's the size of member variables necessary for the string classes function.
GWW
It's the size of the `pointer` to the `class` String. Which in this case seems to be on a 32 bit integer.
Ivo Wetzel
@Kevin: It means that when you write `string foo`, it takes up four bytes on the stack. The actual characters of the string are not part of the string object itself; they're in dynamic memory that the string allocates as needed.
Chuck
So what's the limit as to how many characters I can store in a string:example string a = "aksldjfkljklj234kljk123;4jklj123kj4k;jsdkl;jfkjsdklfjsdfsdf" is legal?
Kevin
Yep, that's legal though you'd be incarcerated for having a user read that.
Graham Perks
@Ivo: That's not correct. It's the size of the instance, which happens to contain nothing more than a pointer to dynamic memory.
Steven Sudit
+1  A: 

Not at all. It means that the class's structure is that, it doesn't include the dynamic memory it can control. std::string will expand dynamically to meet any required size.

s.max_size() // will give the true maximum size
s.capacity() // will tell you how much it can hold before resizing again
s.size() // tells you how much it currently holds

The 4 you get from sizeof is likely a pointer of some kind to the larger structure. Although some optimizations on some platforms will use it as the actual string data until it grows larger than can fit.

Scott Stafford
A: 

A string object contains a pointer to a buffer on the heap that contains the actual string data. (It can also contain other implementation-specific meta-information, but yours apparently doesn't.) So you're getting the size of that pointer, not the size of the array it points to.

dan04
With GCC's implementation std::string consists of a single pointer into an overallocated struct that keeps size, capacity and buffer all together in the same continuous memory block. Probably not uncommon in some other implementations either.
UncleBens
Microsoft uses that approach for their `CString` class, but surprisingly not for their version of `std::string`.
dan04
A: 

No, that's the class, but if you wanted to find the size of a specific string you could do something like this:

std::cout << "size of string: " << sizeof(mystring)/sizeof(string);
Brett
This wouldn't necessarily give you the "real" size of the string, though - just the size of the statically allocated memory for it. So it wouldn't tell you how much memory the string had allocated or anything.
Colen
+4  A: 

Assuming string is defined as

char *string;

Your sizeof is telling you the size of the pointer. 4 bytes. You're on a 32-bit machine. You've allocated no memory yet to hold the actual text. You want a 10-char string? string = malloc(10); Now string points to a 10-byte buffer you can put characters in.

sizeof(*string) would be 1. The size of what string is pointing to, a char.

If you instead did

char string[10];

sizeof(string) would be 10. It's a 10-byte array. sizeof(*string) would be 1 still.

It'd be worth looking up and understanding the __countof macro.

Update: oh, yeah, NOW include the headers :) 'string' is a class whose instances take up 4 bytes, that's all that means. Those 4 bytes could point to something far more useful, such as a memory area holding more than 4 characters.

You can do things like:

string s = "12345";
cout << "length of String " << s.length();
Graham Perks
+6  A: 

It isn't clear from your example what 'string' is. If you have:

#include <string>
using namespace std;

then string is std::string, and sizeof(std::string) gives you the size of the class instance and its data members, not the length of the string. To get that, use:

string s;
cout << s.size();
Ori Pessach
+1  A: 

I know a lot of people had answered your question, but here is some points:

  1. It's not the size of the string or the capacity of the string, this value represents the structural size of the class string, which you can see by its implementation (and it can change from implementation to implementation) that is a simple pointer;
  2. As the sizeof(string) is the size of the class structure, you'll get the size of the only internal pointer, that in your case is 4 bytes (because you are in a 32-bit machine, this can change from platform to platform too);
  3. This pointer inside the string class, points to a memory buffer where the class will hold the real string data, this memory buffer is reallocated as needed, it can increase/decrease as you append/delete/create more string text;
  4. If you want to get the real size of the string, you need to call the size() method from the class which will check the memory buffer string size (which isn't the same as the memory buffer size).

I think your problem is your conception of sizeof, see more information here and here is some explanation on how it works.

Tarantula
A: 

sizeof(char) is always 1 byte. A byte which we think is 8-bits need not be the case. There are architectures where a BYTE is 32-bits, 24-bits and so on. The sizeof applied to any other type is in multiples of sizeof(char) which is by definition 1.

The next important thing to note is that C++ has three character types: plain char, signed char and unsigned char. A plain char is either signed or unsigned. So it is wrong to assume that char can have only values from 0 to 255. This is true only when a char is 8-bits, and plain char is unsigned.

Having said, that assuming that 'string' is 'std::namespace', sizeof(string) == 4 means that the sizeof the 'std::string' class is 4 bytes. It occupies 4 times the number of bytes that a 'char' on that machine takes. Note that signed T, unsigned T always have the same size. It does not mean that the actual buffer of characters (which is called string in common parlance) is only 4 bytes. Inside the 'std::string' class, there is a non static member pointer which is allocated dynamically to hold the input buffer. This can have as many elements as the system allows (C++ places no restriction on this length). But since the 'std::string' class only holds the pointer to this potentially infite length buffer, the sizeof(std::string) always remains the same as sizeof pointer on the given architecture which on your system is 4.

Chubsdad