Path: ns-mx!uunet!cis.ohio-state.edu!ucbvax!pro-calgary.cts.com!qd From: qd@pro-calgary.cts.com (Quinn Dunki) Newsgroups: comp.sys.apple2 Subject: HELP! Message-ID: Date: 26 Feb 92 05:20:46 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: ProLine [pro-calgary] Lines: 50 I have a question for any C programmers out there. Could someone please tell me why this code fragment doesn't work? ------------------------------------------------------------------------ FILE *k; int c,zero[25400]; printf("\n\n\n\n\n\n\n\n\nHit a key...\n"); getchar(); for (c=0; c<25400; c++) zero[c]=0; printf("\n\n\n\n\n\n\n\n\nHit another key...\n"); getchar(); printf("Opening......\n"); k=fopen("xobjnew1.dxd","w+"); getchar(); if (k!=NULL) { for (c=0; c<25400; c++) { fwrite(&zero,2,1,k); printf("Writing #%d\n",c); fclose(k); } } else { fclose(k); printf("\nERROR!\n"); } -------------------------------------------------------------------- When run, it crashes to a fatal system error 308 after the first printf() and getchar(). In other words, it crashes somewhere in the loop that assigns the array "zero". All this is supposed to do is write 25400 0's (in integer form) to a file named "xobjnew1.dxd". Can anyone help? (By the way, this is for the Ultima VI clone, if that inspires any of you to help more! :) :) ) ||||||||||||||||||||||||||||||||||||||||||||||| PLEASE RESPOND TO ONE THESE ADDRESSES: ProLine: qd@pro-calgary Internet: qd@pro-calgary.cts.com UUCP: crash!pro-calgary!qd ||||||||||||||||||||||||||||||||||||||||||||||| \Want me to finish my Ultima VI GS or my / \Knights of Legend GS? Mail Me! ][ Infinitum/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Path: ns-mx!hobbes.physics.uiowa.edu!zaphod.mps.ohio-state.edu!mips!mips!munnari.oz.au!comp.vuw.ac.nz!actrix!David.Empson From: David.Empson@bbs.actrix.gen.nz Newsgroups: comp.sys.apple2 Subject: Re: HELP! Message-ID: <1992Feb27.132225.38@actrix.gen.nz> Date: 27 Feb 92 13:22:25 GMT References: Sender: David.Empson@actrix.gen.nz (David Empson) Organization: Actrix Information Exchange Lines: 52 In article qd@pro-calgary.cts.com (Quinn Dunki) writes: > I have a question for any C programmers out there. Could someone please > tell me why this code fragment doesn't work? > [fragment removed] If your 'zero' array has been declared inside a function, the C compiler will try to allocate it on the stack. The array is 50800 bytes long. You have 8000 bytes of stack space available. Therefore, the array will be starting at a very strange place in bank zero, and when you try to clear it, you will overwrite system variables, parts of GS/OS, possibly I/O space, and other programs' direct page areas. There are two possible solutions to this: either declare the array as 'static', so it will be allocated in the data segment for ORCA/C's small model (which can contain up to 64k), or the array segment for ORCA/C's large model or for APW C (which is limited only by available memory). The same effect can be achieved by moving the array definition outside of the function. This is necessary if you want other functions to access the array. If the array _has_ been declared outside a function, then I don't know what the problem is. One incidental problem: your write loop is writing out the first array element 25400 times. You should be using: fwrite(&zero[c], 2, 1, k); Two other points: 1. To clear a large area of memory, you can use the memset() function, i.e. memset(&zero[0], 0, sizeof(zero)); Don't forget to include . 2. You can replace the file write loop with a single call to fwrite: fwrite(zero, 2, 25400, k); It seems a little extravagant to define a 25400 element array - you could use a much smaller array, and write it multiple times (e.g. 254 bytes, written 100 times). -- David Empson Internet: David.Empson@bbs.actrix.gen.nz EMPSON_D@kosmos.wcc.govt.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand