How to return array from function?

Posted By: Dalla

How to return array from function? - 10/02/17 08:58

How can I return an array from a function?
This doesn't work...

Code:
bool[] initConditionArray(vars close, vars open, vars high, vars low) {
   
	bool conditionArray[100];
	
	conditionArray[0] = close[0] > close[1];
	conditionArray[1] = close[0] > close[2]);
        ....
}

void run {
        ...
        bool conditionsArray[100] = initConditionArray(close, open, high, low);
        ...
}

Posted By: jcl

Re: How to return array from function? - 10/02/17 09:32

No, that does not work, but this would:

bool* initConditionArray(vars close, vars open, vars high, vars low) {

static bool conditionArray[100];

conditionArray[0] = close[0] > close[1];
conditionArray[1] = close[0] > close[2]);
....
return conditionArray;
}

void run {
...
bool* conditionsArray = initConditionArray(close, open, high, low);
...
}
Posted By: Dalla

Re: How to return array from function? - 10/02/17 10:28

Thanks!

Is it possible to know how big an array can be?
Reading the manual it says "In lite-C the stack has a limited size that is sufficient for many variables, but not for huge arrays".

Zorro crashes without any error message when I size my array as ~1500.
Posted By: jcl

Re: How to return array from function? - 10/02/17 11:01

That's why large arrays should be global or static. If the stack size is exceeded, a program will crash with no error message. There's no function to get an array size, since an array variable is in fact a pointer. So you must store the size in an extra variable.
Posted By: Dalla

Re: How to return array from function? - 10/02/17 11:23

Hmm, but it crashes even if I create the array like this

Code:
static bool conditionArray[1760];

Posted By: jcl

Re: How to return array from function? - 10/02/17 12:21

Maybe the crash has a different reason? 1760 bools is anyway not a really huge array. Can you post the whole script?

Posted By: Dalla

Re: How to return array from function? - 10/02/17 16:29

I solved my issue in another way. Realized that I didn't really need to create the array and all conditions on each new bar, so I now use a switch case statement instead which is a lot more efficient. And it has the benefit of not crashing ;-)
© 2024 lite-C Forums