I think i figured out that functions can't return string properly when they are locally buffered.
Please check this sample out which demonstrates this:
string get_string2(void) { return "AAAAAAAAAA"; // string constant } string get_string(void) { string xtag="AAAAAAAAAA"; // local buf of returned value return xtag; } void test(void) { string a=get_string(); string b=get_string(); string os=StringFormat("a=%s b=%s",a,b); // output incorrect Print(os); a=get_string2(); b=get_string2(); os=StringFormat("a=%s b=%s",a,b); // output correct Print(os); } void OnStart() { test(); }
Output
2012.10.11 21:17:50 strtest (EURUSD,M5) a=AAAAAAAAAA b=AAAAAAAAAA 2012.10.11 21:17:50 strtest (EURUSD,M5) a=AAAAAAAAAA b=(null)
Either stack on return or local buffer itself is corrupted.
Workaround
init string a before assigned by "get_string()"
string get_string2(void) { return "AAAAAAAAAA"; } string get_string(void) { string xtag= "AAAAAAAAAA"; return xtag; } void test(void) { string a=""; string b=""; a=get_string(); b=get_string(); string os=StringFormat("a=%s b=%s",a,b); Print(os); a=get_string2(); b=get_string2(); os=StringFormat("a=%s b=%s",a,b); Print(os); } void OnStart() { test(); }
This is only a workaround. The behaviour of the string class is not normal;nobody expects this. It should be corrected. (Thank you)
asdasdasd
Workaround above seems also not to work in all cases.
Basically i get sporadically (null) strings and only God knows why.
Does problem reproduce on the latest build?
It seems to be fixed
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
have a Problem with StringFormat. The function SOMETIMES writes "(null)" to resulting string and there is no reason why this happens.
Find below the code for a complete script demonstrating this:
What is the secret behind THIS strange behaviour (so far it took me 2 hours to isolate...)