sizeof(std::string) was 16 now 32

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scudemax
    New Member
    • Mar 2008
    • 3

    sizeof(std::string) was 16 now 32

    I am having trouble porting some code from VC6 to VC8 (2005).
    I need to deserialize files that were written in VC6 and heavily use the std::string template.

    sizeof(std::str ing) will be 16 from vc6, but will be 32 from vc8. I need it to be 16.
    see the sample code below:

    CODE
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string s = "";
    int sz;
    sz = sizeof(s);
    printf("strings ize = %d\n",sz);
    return 0;
    }
    /CODE
    Thanks in advance,
    Max
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by scudemax
    I am having trouble porting some code from VC6 to VC8 (2005).
    I need to deserialize files that were written in VC6 and heavily use the std::string template.

    sizeof(std::str ing) will be 16 from vc6, but will be 32 from vc8. I need it to be 16.
    see the sample code below:

    CODE
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string s = "";
    int sz;
    sz = sizeof(s);
    printf("strings ize = %d\n",sz);
    return 0;
    }
    /CODE
    Thanks in advance,
    Max
    I don't understand why you want to use sizeof() on a std::string. Shouldn't you just use the std::string::le ngth() method? The string class may have all sorts of internal stuff inside that you can only hang yourself with if you muck with it.

    Comment

    • scudemax
      New Member
      • Mar 2008
      • 3

      #3
      I don't know why it was done that way either. ...but it was. In order to deserialize the existing data properly, sizeof(std::str ing) must return 16 not 32.
      Any guidance will be appreciated.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        In order to deserialize the existing data properly, sizeof(std::str ing) must return 16 not 32.
        We don't always get what we want. Good programmers recognize not to make certain assumptions that reveal poor judgment. Like basing a serialization system on the assumption that std::string has a sizeof value of 16 and not something else.

        If this is code you must somehow port, it looks like you will be rewriting the serialization functionality. Welcome to the wonderful world of porting or updating poorly written code. I sympathize for you. But you can do nothing about the sizeof value, which is compiler and implementation dependent.

        Comment

        • scudemax
          New Member
          • Mar 2008
          • 3

          #5
          Thanks, that answers my question!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I don't see your problem.

            You say you have a solution based on an implementation of the STL string?

            If so, how was your std:: string declared?

            I have seen:
            [code=cpp]
            class basic_string
            {
            T* str;
            };
            [/code]

            [code=cpp]
            class basic_string
            {
            T arr[20];
            T* str;
            };
            [/code]

            Are you actually saying that someone looked inside the STL and wrote an application that required that exact implementation? ?

            If you answer yes, that persson did you a disservice.

            Finally, since you seem to be caught up in a particular STL implementation, I suggest you remove the VS2008 templates and use the templates you will be happy with. It's really too bad your application is welded to obsolescence.

            Comment

            Working...