CompuServe Thread

#Array filling problem

12 messages in this thread
#43594From: Henry WilliamsOct 30, 1994 7:12 PM
Hello, I could really use some help here with my frustrations. Please! I put 892 records of size struct record in a file "Names", which I want to fill an array in my real program. I wrote the small program below to check if everything will load right at run time and guess what?? I keep getting a GURU 0005 "Memory list damaged" message. This program prints out about 30 names and then hello guru. Thank-you for the help. Henry Williams #include <stdio.h> struct record{ char name[35]; int family; }; main () { FILE *in; struct record box; int a = 0, n = 1; char array [892][35]; if (( in = fopen ("Names", "r")) == (FILE *) NULL ){ fprintf (stderr, "\n Couldn't read Name file!\n"); exit(10); } while (n == 1){ n = fread ((char *) &box, sizeof (struct record), 1, in ); strcpy (array[a], box.name); printf (" %-35s %d\n", array[a], box.family); ++a; } printf (" A = %d N = %d\n", a, n); fclose (in); }
#43609From: Jim Nangano/SYSOPOct 31, 1994 9:48 AM
Henry, The first potential for problem I see is your array allocation — unless you have set your stack fairly large, you will be using more stack space than is allocated, which can lead to all kinds of problems. Your data allocations alone for your main() function are close to 32K, so you will need to have a stack set to at least 40K or so. The second potential for a problem would occur _if_ you had a record in the file where the name element contained 35 characters and no terminating NULL byte, as your program assumes that this could not occur. This may be a valid assumption (depending on how you created the data), but if it _might_ not be so, you would be better off using the strncpy() function, and assuring yourself that the last byte in each array element was a NULL. Hope this helps…. – BobR (Motorola Inside!)
#43673From: Henry WilliamsNov 3, 1994 6:52 PM
Bob, Thanks for the hand! You hit it on the head, or should I say stack. You have also cleared up what has been going wrong in my original program. I was never aware of overflow problems in programming. Fortunately I also found a compiler function that checks the stack for me. I was wondering, is there a way to change the stack size from within a C program? Thanks again, Henry
#43690From: Jim Nangano/SYSOPNov 4, 1994 10:06 AM
Henry, I believe the some of the later incarnations of the Amiga C compilers provided a mechanism to increase the stack size. but it is something that is definitely compiler-specific. Unfortunately, I've done little programming on the Amiga lately and my compiler packages are far from current. You'll have to check the documentation for your compiler package. Good luck…. – BobR (Motorola Inside!)
#43701From: Wayne HannamNov 4, 1994 4:25 PM
You would be better off using the malloc function and the free function to dynamically allocate the array, and free it, this will work better accross platforms and operating systems.
#43621From: Werner KazmierzakNov 1, 1994 12:26 AM
Henry, with strcpy(), the source string has to be null terminated, so at least box.name[34] must be a null byte in each record. Maybe you want to use strncopy() instead. – wkc – … via AP from Hamburg, Germany
#43674From: Henry WilliamsNov 3, 1994 6:53 PM
Werner, Thanks for the advice. The way I saved the data to file I was sure I had NULL terminating strings. As it turned out the problem was too small a stack size. Thanks again, and keep up that great Amiga support in Germany! Henry
#43698From: Steve AhlstromNov 4, 1994 4:05 PM
Don't stuff the array on the stack and you'll do away with the problem and won't inconvience your users into having to change the size of the default stack. Allocate the memory the free it when you're done. -sja
#43815From: Henry WilliamsNov 8, 1994 6:55 PM
Steve, I like your idea of allocating memory to take care of my stack problem. The trouble I think I will have here is that my array is Global. Is it possible to allocate memory for a global array? Thanks for the advice. Henry
#43830From: Steve AhlstromNov 9, 1994 2:21 PM
It doesn't make any difference if your array is global or not. In your initialize routine allocate the memory needed and in your shutdown routine free it — your array will no longer use the stack because it'll have it's own dedicated memory.
#43891From: Doug WalkerNov 12, 1994 9:11 PM
If the array is global, why not simply declare it "static" or move the declaration external to any function? If you do that, the array won't be a local variable, so it won't take up stack space. BTW, SAS/C and I think DICE both provide stack CHECKING code. In SAS/C, this would have brought up a requester when you entered your function telling you that you had exceeded your stack space. Stack checking is on by default with SAS/C. I don't know what compiler you are using, but I suggest looking into the stack checking code provided by your compiler. Lastly, you can adjust the stack your program gets if you are using SAS/C V6.0 or above by declaring an external variable called __stack and initializing it to the number of bytes you want (make sure it's a multiple of 4.) In SAS/C 5.10, the name is _stack, but in that version it only works with the cres.o and cback.o startups. –Doug
#43711From: Werner KazmierzakNov 4, 1994 8:07 PM
Henry, you _can_ swap to a bigger stack from within your C program. With OS 2.0 or better, you may use the exec.library function SwapStack((StackSwapStruct *) newStack) (V37) A0 With KS 1.3, you'll have to write your own assembly function. I don't know why this function isn't listed in the 3rd edition RKM Autodocs, but you can read all about it in the Autodocs_3.1. – wkc – … via AP from Hamburg, Germany