Connecting Tech Pros Worldwide Forums | Help | Site Map

How many bytes in a string?

Newbie
 
Join Date: Aug 2008
Posts: 1
#1: Aug 27 '08
Hi,
I am a total n00b in C++ and have a basic question:

How big is a string:

I have discovered the following:

bool - 1 byte
char - 1 byte
int - 2 bytes
short int - 2 bytes
long int - 4 bytes
float - 4 bytes
double - 8 bytes

then theres string... does it have a fixed size?

and what about enum? Its a type as well, isnt it?

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#2: Aug 27 '08

re: How many bytes in a string?


A C++ string object can be implemented in various ways. Usually you have to use a method on the string object to get its size. Size of a string is usually the number of characters (not bytes) in the string. That means you can't use sizeof since sizeof only reports the memory occupied by the variable on the stack frame.

A C string is a char array with a binary zero (\0) as the final char. In this case you use strlen() tp get the number of characters. Here, again, sizeof will not work unless the array is local to the function or is a global array.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#3: Aug 27 '08

re: How many bytes in a string?


Quote:

Originally Posted by lifehacker

I have discovered the following:

bool - 1 byte
char - 1 byte
int - 2 bytes
short int - 2 bytes
long int - 4 bytes
float - 4 bytes
double - 8 bytes

The only one of these statements that is actually correct is

char - 1 byte

This is guaranteed by the C and C++ standards. A lot of your other statements are true in many many cases but the size of most types is actually platform dependent.

So if you had said "I have discovered the following about my platform" then you would have been correct (possibly).

I am surprised you find int to be 2 bytes. Since you are new to C++ programming I would image you are using a PC and most modern compilers give an int of 4 bytes on a PC.
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#4: Aug 28 '08

re: How many bytes in a string?


As weaknessforcats said, using the sizeof operator, like this:
sizeof(string)
will give you the amount of space that a string object takes up on the stack on your particular platform, that is, the amount of space that string objects are guarunteed to take up, even before you put any characters into them. But string objects, unlike the other types you have been finding the size of, have space allocated on the heap, which is where the characters are stored. There is no easy way of finding out how much space a string object is taking up on the heap, and it can change while your program is running, as you add and remove characters from the string.

enum is not really a type. It's more of a way of defining your own types. However, when you declare a variable to be of a type that you have defined with enum, I think it's the same size as an int.

Did you find out the sizes in your table with the sizeof operator? If you didn't, try using the sizeof operator on all those types to see if it matches what's in your table. Then you can find out what the sizes of strings and enum types are too.

Hope this helps.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#5: Aug 28 '08

re: How many bytes in a string?


Quote:

Originally Posted by boxfish

enum is not really a type. It's more of a way of defining your own types. However, when you declare a variable to be of a type that you have defined with enum, I think it's the same size as an int.

Size of enum is platform dependent. Some platforms will chose the smallest integer type necessary to hold the defined values of the enums members, others will make it an int unless 1 or more defined values of its members wont fit in an int in which case a long might be used.


Another thing to remember is that int short, long char, float double etc are built in types of the language. string is not a built in type it is a class defined in the class library which is why they have to be handled differently.
Reply