473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

size of string

A text book says "a string's size is independent of the length of the
string it addresses". Why?
For example,
string st1( "foobar" );
string st2( "a mighty oak" );
cout << "st1: " << sizeof( st1 )
<< " st2: " << sizeof( st2 );

The output is:
st1: 12 st2: 12

Why? The length of any string is 12?

Thanks.

Johm
Jul 22 '05 #1
5 2644
"John" <jo*********@yahoo.com> wrote in message
A text book says "a string's size is independent of the length of the
string it addresses". Why?
For example,
string st1( "foobar" );
string st2( "a mighty oak" );
cout << "st1: " << sizeof( st1 )
<< " st2: " << sizeof( st2 );

The output is:
st1: 12 st2: 12

Why? The length of any string is 12?


On your implementation, yes. The sizeof(any class) is the sum of the sizeof
of each of its data members. A non reference counted string normally has 3
data members: (1) a pointer to the data usually of type char* and
sizeof(char*) is 4 on your implementation, (2) an integer holding the length
of the string, (3) an integer holding the length of the data buffer.
Jul 22 '05 #2
John wrote:
A text book says "a string's size is independent of the length of the
string it addresses". Why?
For example,
string st1( "foobar" );
string st2( "a mighty oak" );
cout << "st1: " << sizeof( st1 )
<< " st2: " << sizeof( st2 );

The output is:
st1: 12 st2: 12

Why? The length of any string is 12?

Thanks.

Johm


Since your string object only holds a pointer to the actual string data, and
since pointers are usually 4 bytes long on 32-Bit architectures, the size
of your two string objects is the same.

Think of an std::string to be a more convenient way to handle character
arrays, instead of an actual string. Maybe that helps.

- Matthias
Jul 22 '05 #3
Siemel Naran wrote:
"John" <jo*********@yahoo.com> wrote in message
A text book says "a string's size is independent of the length of the
string it addresses". Why?
For example,
string st1( "foobar" );
string st2( "a mighty oak" );
cout << "st1: " << sizeof( st1 )
<< " st2: " << sizeof( st2 );

The output is:
st1: 12 st2: 12

Why? The length of any string is 12?

sizeof gives you the size of the class, which is always a compile-time
constant. This is not limited to strings, but rather is true for every data
type. What the book refers to as "size" is not the number of characters
stored (which you would get with the size() member function), but the value
of sizeof.
On your implementation, yes. The sizeof(any class) is the sum of the
sizeof of each of its data members.


Not really. There can be alignment, making the class bigger than its data
member sizes added together. Try it with something like:

class X
{
public:
char a;
int b;
};

On many compilers, sizeof(X) will be two times sizeof(int), even though a
char is usually smaller than an int.
Also, classes that contain virtual functions ususally get the size of one
pointer added.

Jul 22 '05 #4
Siemel Naran wrote:

On your implementation, yes. The sizeof(any class) is the sum of the sizeof
of each of its data members.


It might be bigger than that. However, sizeof (some_class) is always
a constant that most likely is the sizeof all the non-static data members
plus possibly a small bit of overhead.
Jul 22 '05 #5
Rolf Magnus <ra******@t-online.de> wrote in message news:<cn*************@news.t-online.com>...
Siemel Naran wrote:
"John" <jo*********@yahoo.com> wrote in message
A text book says "a string's size is independent of the length of the
string it addresses". Why?
For example,
string st1( "foobar" );
string st2( "a mighty oak" );
cout << "st1: " << sizeof( st1 )
<< " st2: " << sizeof( st2 );

The output is:
st1: 12 st2: 12

Why? The length of any string is 12?


sizeof gives you the size of the class, which is always a compile-time
constant. This is not limited to strings, but rather is true for every data
type. What the book refers to as "size" is not the number of characters
stored (which you would get with the size() member function), but the value
of sizeof.
On your implementation, yes. The sizeof(any class) is the sum of the
sizeof of each of its data members.


Not really. There can be alignment, making the class bigger than its data
member sizes added together. Try it with something like:

class X
{
public:
char a;
int b;
};

On many compilers, sizeof(X) will be two times sizeof(int), even though a
char is usually smaller than an int.
Also, classes that contain virtual functions ususally get the size of one
pointer added.


Thanks.

John
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
79
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
6
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following....
2
by: Phil Galey | last post by:
Using the following, you can determine the size of a file: Dim fi As New IO.FileInfo(<Path to file>) MsgBox(fi.Length) .... but what about the size of a directory? The IO.DirectoryInfo object...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
2
by: Dominique | last post by:
Hello, Im want to fix the width of a dashboard. I have a field which has a field (in this dashboard on the left part of my screen) which could be long and make the right side of the screen out...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
6
by: Marcolino | last post by:
Hi all, I have a simple problem but I don't know which is the best way to solve it. I need to savo into a table into access DB the location and the size of a form. Is there a way to save...
7
by: Luna Moon | last post by:
#include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { string cc(31, 'c'); string bb=cc.assign(3, 'dd');
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.