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:
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!
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!