Re: Concactinating Strings
Are you able to return a string + using a string parameter at all? I had crashing issues with that before... I'm not really an advanced programmer so I don't think I can help you. I just know that ReturnString expects a const char*... but that shouldn't make it crash.
Re: Concactinating Strings
Two possible errors:
1. When returning a string, you must set the correct flags to make mmf understand it's a string.
2. If you create a std::string and return it (or actually the address to the string, c_str()), the object will be destroyed and mmf will try to access freed memory.
Re: Concactinating Strings
There is some function you can call to get MMF to allocate the memory for the string you are going to return, and MMF will free it once it's done with it. You should use that.
Re: Concactinating Strings
What function should I use?
Re: Concactinating Strings
if you use rSDK, ReturnStringSafe(str); should work...
Re: Concactinating Strings
My code:
Code:
EXPRESSION(
/* ID */ 0,
/* Name */ "Name(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (1,EXPPARAM_STRING,"Parameter")
) {
char * str1 = (char *)"1st String"
char * str2 = (char *)ExParam(TYPE_STRING);
const char * str3 = strcat(str1,str2);
ReturnStringSafe(str3);
}
I test this by creating a String Object and, on startup, setting it to my object's expression using a random parameter. The application crashes at the start of the frame.
Re: Concactinating Strings
You can't do that. str1 is set to a constant, in write protected memory. It should crash on strcat.
You could use strdup("1st String").
Re: Concactinating Strings
Sorry, I don't understand what to do with the function you gave me. Where do I put it?
Re: Concactinating Strings
Quote:
Originally Posted by Jaffob
char * str1 = (char *)"1st String"
Missing a semi-colon :)
(Probably not the solution)
Re: Concactinating Strings
My code has a semicolon ;). When I copied it in I must have accidentially removed it.
If there was no semicolon, the code itself would not compile.
Re: Concactinating Strings
I know that, just pointing it out
Re: Concactinating Strings
Code:
EXPRESSION(
/* ID */ 0,
/* Name */ "Name(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (1,EXPPARAM_STRING,"Parameter")
) {
char * str1 = (char *)"1st String";
char * str2 = (char *)ExParam(TYPE_STRING);
char * str3 = rdPtr->rRd->GetStringSpace(strlen(str1) + strlen(str2) + 1);
strcpy(str3, str1);
strcat(str3, str2);
ReturnString(str3);
}
Re: Concactinating Strings
Raw strings in C are a pain...
But you could also use C++ CStrings like this:
Code:
EXPRESSION(
/* ID */ 0,
/* Name */ "Name(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (1,EXPPARAM_STRING,"Parameter")
) {
CString str1 = "1st String";
CString str2 = (char *)ExParam(TYPE_STRING);
CString str3 = str1 + str2;
ReturnStringSafe(str3.c_str());
}
Re: Concactinating Strings
With Dynasoft's code my extension does not compile. It seems like CString has not been included. How can I include it?
Re: Concactinating Strings
Can you tell I haven't used them for a while :P
I got CString (a microsoft specific thing) and string (the C++ standard one) confused. Replace all the CString in my code with string, and add:
EDIT: So this:
EDIT: No + operator, only += apparently, so change like this:
EDIT3: Scratch that last edit
Code:
#include <string>
EXPRESSION(
/* ID */ 0,
/* Name */ "Name(",
/* Flags */ EXPFLAG_STRING,
/* Params */ (1,EXPPARAM_STRING,"Parameter")
) {
string str1 = "1st String";
string str2 = (char *)ExParam(TYPE_STRING);
string str3 = str1 + str2;
ReturnStringSafe(str3.c_str());
}
Re: Concactinating Strings
There is a + operator for std::string.
Re: Concactinating Strings
It wasn't listed in the online c++ help I looked at.
EDIT: Changed my post back.
Re: Concactinating Strings
Thanks everyone (Especially Dynasoft for his code)! It works now.