user3452589
user3452589

Reputation: 39

Different value when converting char*

I have to convert token[1] to vm_address_t, but when doing so the original value is lost. Any idea how to convert / cast it right ?

Here is my code:

char* ConvertToC(string value){
    char *cvalue = &value[0u];
    return cvalue;
} 

const char* getOffsetToken(string value){
    const char *offsettoken;
    int n = 0;

    const char* token[4] = {};

    token[0] = strtok(ConvertToC(value), " ");
    if (token[0]) {
        for (n = 1; n < 4; n++) {
            token[n] = strtok(0, " ");
            if (!token[n]) break;
        }
    }
    offsettoken=token[1];
    return offsettoken;
}

int main(){
    vm_address_t vmp;
    const char* cp;
    string p1 = "1 0x1000 2 0x0120";

    cout << getOffsetToken(p1)<<endl;
    cp=getOffsetToken(p1);
    cout << cp<<endl;
    vmp<<(vm_address_t)cp;
    cout << vmp<<endl;
}

The output is:

0x10 ?
0x1000
140734705163168

Upvotes: 0

Views: 69

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

This

 char* ConvertToC(string value){

should be a reference parameter

 char* ConvertToC(string& value){
                     // ^

Otherwise the address returned refers to a temporary copy and turns invalid after the function call.

Upvotes: 1

Related Questions