Contents changed(cleared?) when access the pointer returned by std::string::c_str()

Posted by justamask on Stack Overflow See other posts from Stack Overflow or by justamask
Published on 2010-05-14T11:19:28Z Indexed on 2010/05/14 11:34 UTC
Read the original article Hit count: 207

Filed under:

    string conf()     {         vector v;         //..         v = func(); //this function returns a vector

        return v[1];     }

    void test()     {         const char* p = conf().c_str();         // the string object will be alive as a auto var         // so the pointer should be valid till the end of this function,right?           // ... lots of steps, but none of them would access the pointer p

        // when access p here, SOMETIMES the contents would change ... Why?         // the platform is solaris 64 bit         // compiler is sun workshop 12         // my code is compiled as  ELF 32-bit MSB relocatable SPARC32PLUS Version 1, V8+ Required         // but need to link with some shared lib which are ELF 32-bit MSB dynamic lib SPARC Version 1, dynamically linked, stripped     }

© Stack Overflow or respective owner

Related posts about c++