Support Board
Date/Time: Sun, 09 Mar 2025 13:59:10 +0000
[Programming Help] - corruption of variables on simple code using standard arrays
View Count: 595
[2022-03-01 09:02:21] |
User824913 - Posts: 15 |
After a day of testing with one of my studies, I isolated a problem that I did not expect. Here I am repeating a simple code that shows the anomaly // The top of every source code file must include this line #include "sierrachart.h" SCDLLName("Provette") bool flag=false; int tempi[100]; int timeInMilliseconds; SCSFExport scsf_Provette(SCStudyInterfaceRef sc) { // Section 1 - Set the configuration variables and defaults if (sc.SetDefaults) { sc.GraphName = "Provette"; sc.AutoLoop = 1; //Automatic looping is enabled. sc.Subgraph[0].Name = "Name"; sc.Subgraph[0].DrawStyle = DRAWSTYLE_LINE; sc.Subgraph[0].PrimaryColor = RGB (0, 255, 0); sc.Input[0].Name = "Float Input"; sc.Input[0].SetFloat(0.0f); return; } SCString sTxt; sc.AddMessageToLog(sTxt.Format("flag--> %d",flag),0); int timeInMillisecondsTmp = sc.CurrentSystemDateTimeMS.GetTimeInMilliseconds(); int lassoTempo=timeInMillisecondsTmp-timeInMilliseconds; timeInMilliseconds=timeInMillisecondsTmp; tempi[0]=lassoTempo; double tTot=0; for (int i=99;i>=0;i--){ tempi[i+1]=tempi[i]; tTot=tTot+tempi[i]; } double tempoMedio=tTot/100.0; bool test=true; } Where is the problem? thks a lot |
[2022-03-01 12:06:47] |
1+1=10 - Posts: 270 |
It appears you never modify “flag” within this study. The only thing I can think of is that “flag” is a global variable. Thus, it gets shared between all the other studies within SCDLLName("Provette"). So if any other custom study modifies “flag” it would be seen by this study too. Date Time Of Last Edit: 2022-03-01 12:20:22
|
[2022-03-01 12:19:16] |
1+1=10 - Posts: 270 |
If this is your problem and you want data, specific to a study, to persist between cycles, you need to use persistent variables and/or dynamic memory allocations and/or sc.StorageBlock: ACSIL Interface Members - Functions: Persistent Variable Functions ACSIL Programming Concepts: Dynamic Memory Allocations Within Study Instance ACSIL Interface Members - Variables and Arrays: sc.StorageBlock |
[2022-03-01 13:23:21] |
User907968 - Posts: 838 |
for (int i=99;i>=0;i--){ tempi[i+1]=tempi[i]; tTot=tTot+tempi[i]; There is an array bounds error during the first iteration of the for loop. iter 1: i = 99 therefore: i + 1 = 100 100 is beyond the end of the array, so this is undefined behavior. |
[2022-03-01 13:26:27] |
1+1=10 - Posts: 270 |
@User907968 100 is beyond the end of the array, so this is undefined behavior. Nice catch. |
[2022-03-01 15:10:26] |
User824913 - Posts: 15 |
Ufff... what a gross mistake. I lost a day without realizing it. Sorry, thanks |
[2022-03-01 15:36:35] |
1+1=10 - Posts: 270 |
@User824913. It happens! I went to bed last night with a bug in my ACSIL that resulted from: // This is fine. if (....) x = 1; // In Python both statements would be inside the if but in C++ the 2nd statement is not. // Typically I'd catch this but I guess I was tired. if (....) x = 1; y = 2; // Not inside the if Anyway, good luck in your trading! |
To post a message in this thread, you need to log in with your Sierra Chart account: