String interpolation crashing Zorro

Posted By: neosb

String interpolation crashing Zorro - 11/14/15 18:22

Hey, I have a code like this:

Code:
action = "";
strcat(action, "Strategy/ghost.py ");
strcat(action, strf("'%s' ", Asset));
strcat(action, strf("%d ", Money));
strcat(action, strf("%d ", ExitTime));
strcat(action, "'90% / 0%' ");
strcat(action, "'put'");
printf("\nCreated action...");
exec(action_file, action, 1);
printf("\n%s\n\n", action);
Sleep(33);



This is a part of a function that is called when the trade is executed. Unfortunately it's failing with a crash in script and inconsistent series calls, sometimes in other configurations it gives a trade in one direction but I'm never able to repeat a trade in that direction with another crash in script. Please tell me how should I create a string that is recreated every time I place a trade.

There is no problem with exec function, it's called properly, the output of current stage is failing and printing my string, but not "Created action...". I'm not good with C, I've always worked on higher-level languages starting with C++ and I'm not clearly sure how to go about this string interpolation...

What is strange, is that this code works well when the function is called just once, like you would make a separate program with just that code and a global string action, but this is not the case.
Posted By: Petra

Re: String interpolation crashing Zorro - 11/15/15 09:25

Look here:

action = "";
strcat(action, "Strategy/ghost.py ");

strcat will not make a string longer, it will crash when that happens because you're wrinting randomly into memory and this way overwrite maybe pointers and variables.

It would work when you have an enough long string, like

char action[1000]

Ohterwise better use strf for making a string, this makes your code much shorter, and strcat is anyway an old and outdated C function.

© 2024 lite-C Forums