yin yang
yin yang

Reputation: 229

String to char* conversion fails for non static string

Note : The API uses char* and not const char * so I cannot use c_str() also the function call sendCall is an Aysnchronous call

Below is my code it works fine when I use

char* payloadString = >&currString[0]; 

however when I try using

char* payloadString = &secondCopy[0];

It fails and I am not able to able to understand the reason. I want to create a dynamically updateable string like desiredString in below code which takes version variable and assign it to a static string secondCopy and be able to use that instead of currString but I guess I am making some mistake with address of operator by using a modified static string. Please suggest a work around.

void functionName()
{
    std::string version;
    version = "ABC";
    static std::string  currString= "<Version=\"3.0\" Ret=\"false\"/>";
    std::string desiredstring= "<Version="+version+" Ret=\"false\"/>";
    static std::string secondCopy = desiredstring;
    char* payloadString = &currString[0];
    //char* payloadString = &secondCopy[0];

    XDSC::Definition aDefinition("sname", "sid");

    try
    {
            std::auto_ptr<otf::ClientSession> aSession;
            aSession = getResources()->getManager().getSession("XML");
            aSession->setAttachedToServerConversation(true);
            aSession->setLogicalName("xyz");
            aSession->setITOReturnPackage("svrc");
            boost::shared_ptr<Payload> aPayload = boost::make_shared<Payload>(aDefinition, "3.0",payloadString, strlen(payloadString));
            sendCall(aSession.get(), aPayload,"SRING", true);

    }
    catch(std::exception& e)
    {
        throw (exception(ERROR,"SendCall Failed to Send"));
    }
}

Upvotes: 1

Views: 183

Answers (2)

Yuri
Yuri

Reputation: 351

Even if secondCopy is static (just like curString), you are assigning to it a local variable every time you call functionName. This means that the underlying pointer to the char* inside the string object may change between two calls to the function. If the sendCall function is asynchronous or if you save the pointer somewhere for later use, you may find it invalid if you called the function another time.

Example:

char* foo(string s)
{
    static string ss = "aaa";
    ss = s; //assignment operator, the char* inside the string class may be reallocated
    char* pointer = &ss[0];
    return pointer;
}

void main()
{
    string s1 = "a";
    char* p1 = foo(s1); //p1 is OK for now
    string s2 = "b";
    char p2* = foo(s2); //p1 is probably NOT OK

    return 0;
}

Upvotes: 2

Alvaro Denis Acosta
Alvaro Denis Acosta

Reputation: 815

If you ere running in paralell the sendCall function:

sendCall(aSession.get(), aPayload,"SRING", true);

aPayload become from payloadString and payloadString become from &currString[0]; (is recommended to use c_str() member function, THIS NO SOLVE YOU PROBLEM HERE) so when functionName is going out of the scope the currString variable automatically call his destructor and char* payloadString = &currString[0]; become as an invalid pointer.

Upvotes: 0

Related Questions