Thread: sizeof string?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    sizeof string?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    	std::string s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    	std::string s2 = "a";
    	cout << sizeof(s1) << endl;
    	cout << sizeof(s2) << endl;
    	cin.get();
    }
    Why does sizeof give me 32 for both strings?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why shouldn't it? sizeof is not .size(), after all.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Why does sizeof give me 32 for both strings?
    What you're looking for is s1.length() or s1.size(). sizeof gives you the sizeof the data structure, in this case the size of the string class. For example (sizeof int) would tell you how many bytes an int is on your system. Or cout << std::numeric_limits<int>::max() would print the maximum value an int can contain.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    strlen()

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    strlen()
    No. strlen() is for C-strings (char *, not std::string).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I actually saw some developers at a previous job use:
    Code:
    strlen( something.c_str() );
    Luckily I was on the other side of the building when I was reviewing the code, so they couldn't hear me laughing my ass off.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Quote Originally Posted by cpjust View Post
    I actually saw some developers at a previous job use:
    Code:
    strlen( something.c_str() );
    Luckily I was on the other side of the building when I was reviewing the code, so they couldn't hear me laughing my ass off.
    Looks like they learned C++ using Visual Studio 7 or something. If I recall correctly there was a time when M$ compilers didn't implement the string class completely

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Such a lack in the implementation could be hard to believe, though

    Anyway, here's my homemade string class. It is a lot safer than std::string, but it misses a few features that would make it complete:

    Code:
    class String
    {
        public:
        unsigned capacity() const { return 0; }
        bool empty() const { return true; }
        unsigned find( const String& str, unsigned index ) const { return std::numeric_limits<unsigned>::max(); }
        unsigned size() const { return 0; }
        //TO_DO: add other methods
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by pantherse View Post
    Looks like they learned C++ using Visual Studio 7 or something. If I recall correctly there was a time when M$ compilers didn't implement the string class completely
    Well the std::string in VC++ 6.0 seems fairly correct. I haven't used anything earlier than that.

    Quote Originally Posted by anon View Post
    Such a lack in the implementation could be hard to believe, though

    Anyway, here's my homemade string class. It is a lot safer than std::string, but it misses a few features that would make it complete:

    Code:
    class String
    {
        public:
        unsigned capacity() const { return 0; }
        bool empty() const { return true; }
        unsigned find( const String& str, unsigned index ) const { return std::numeric_limits<unsigned>::max(); }
        unsigned size() const { return 0; }
        //TO_DO: add other methods
    };
    LOL!

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Anyway, here's my homemade string class. It is a lot safer than std::string, but it misses a few features that would make it complete
    I thought the std::string is pretty safe?...

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There are some problems because it has to interface with C-style strings. E.g

    Code:
    std::string s(0);
    I'm not sure if this is required to throw an exception.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not sure if this is required to throw an exception.
    It is not allowed by the pre-condition, but the behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM