Dynamic array crashes zorro

Posted By: Smon

Dynamic array crashes zorro - 04/22/18 06:11

I finally managed to implement a dynamic array. Pretty simple and straightforward, but Zorro keeps crashing randomly. Here is a demo code:

Code:
typedef struct {
	var variable1;
	var variable2;
	var variable3;
	var variable4;
	int variable5;
	int variable6;
	var variable7;
	var variable8;
	int variable9;
} MYSTRUCT;	
	


static int arraysize;

int i;	
	
int main()
{
MYSTRUCT *dynarray, *temp;
arraysize = 8; //start small
dynarray = malloc(arraysize*sizeof(MYSTRUCT));
//int increment = 20;

	for (i = 1; i < 65536; i++){
		
		
		
		if(i >= arraysize)
		{
			temp = realloc(dynarray,(arraysize*2)*sizeof(MYSTRUCT)); /* double array size */
			arraysize = arraysize*2;
			printf("nsize of array increased to %d elements!", arraysize);
			if ( temp != NULL )
			{
				dynarray = temp;
			}
				else			
			{
				free(dynarray);
				printf("nError allocating memory! Support and Resistance Indicatorn");
				printf("nFatal Error!!!");
			}
		}
		
		dynarray[i].variable5 = i; //save some value
		printf("ni = %d; dynarray[1]variable5 = %d; arraysize = %d", i, dynarray[i].variable5, arraysize);
	}
	
	for (i = 0; i < arraysize; i++) printf("n%d", dynarray[i].variable5);
	
	free(dynarray);
}



I need the struct for my project and just randomly picked one of the int variables (variable5) to demonstrate that the array actually is functioning.

What I don't understand is that I never get the Error message. Zorro just crashes by not responding anymore.

I'm thankful for any suggestions to make this work!
Posted By: Smon

Re: Dynamic array crashes zorro - 04/22/18 06:14

The issue seems not related to the realloc function. If I start with

Code:
arraysize = 65536;



it keeps crashing anyway.
Posted By: Brax

Re: Dynamic array crashes zorro - 04/22/18 11:09

I haven't had any succeed with dynamic arrays in Zorro either. They are tricky in standard C, so who knows what happens inside Lite-C...

Better off using an oversized static array or a series.
Posted By: Smon

Re: Dynamic array crashes zorro - 04/22/18 15:11

My concern is, that a static array of some thousand elements will crash Zorro as well, and I need one of these arrays for each of the 27 currency pairs. Looks like the only option is using R...?
Posted By: Smon

Re: Dynamic array crashes zorro - 04/22/18 15:22

a large static array with 1000 elements of the type MYSTRUCT works, but 1500 elements is already too much and produces Error 111...

So I'm better off with the dynamic array.... cry
Posted By: Brax

Re: Dynamic array crashes zorro - 04/23/18 09:31

Well, maybe it's an internal limitation... jcl should know that.

If you don't really need to keep all the data at the same time, you could use a series and shift it whenever you want.

http://www.zorro-trader.com/manual/en/series.htm

Hope it helps.
Posted By: Smon

Re: Dynamic array crashes zorro - 04/23/18 12:55

I already thought about that, but I really do need everything in memory. Thx!
Posted By: Smon

Re: Dynamic array crashes zorro - 04/23/18 14:08

It turns out that the printf loop is the culprit. Zorro obviously has a problem when you print out too much stuff to the console....!
© 2024 lite-C Forums