Size of string object???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Size of string object???

 
0
  #1
Jul 4th, 2006
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';
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Size of string object???

 
1
  #2
Jul 4th, 2006
>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:
  1. template <class T, class Traits = char_traits<T>, class Alloc = allocator<T> >
  2. class basic_string {
  3. public:
  4. // Lots and lots of interface stuff
  5. private:
  6. size_type size; // Probably a 32-bit type
  7. size_type capacity; // Probably a 32-bit type
  8. _buf_type buffer;
  9. };
Where _buftype is likely defined as something along these lines:
  1. struct _buf_type {
  2. T *base; // Beginning of buffer
  3. T *mark; // Helper pointer within buffer
  4. };
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Size of string object???

 
0
  #3
Jul 5th, 2006
Thanks Narue,

Is there any function to calculate the length of string object. From ur explanation I guess strlen wont work.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Size of string object???

 
0
  #4
Jul 5th, 2006
Yes, you find the manual / help pages which describe the string class.
This will detail the public interface to the class.

Say perhaps
  1. string foo;
  2. foo.length();
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Size of string object???

 
0
  #5
Jul 5th, 2006
Thanks Salem.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Size of string object???

 
1
  #6
Jul 5th, 2006
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.
Last edited by Narue; Jul 5th, 2006 at 9:26 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC