Article View: comp.sys.amiga.tech
Article #13238Memory allocation
From: George Armhold
Date: Sat, 30 Dec 1989 18:09
Date: Sat, 30 Dec 1989 18:09
76 lines
2723 bytes
2723 bytes
I've a rather neophyte question for the net:
I have a program that employs a recursive routine to traverse a
directory structure on a disk. If I have the program simply traverse
the structure and "visit" each file and directory, the program works
fine. But when I try to copy each file I find, the program crashes the
machine, usually with a "Task held" requester. Here is the copyfile()
function I call whenever I find a file:
void copyfile(source, destination)
char *source, *destination;
{
int file1, file2; /* file descriptors */
int n;
char buf[1024]; /* buffer for data transfer */
if ((file1=open(source, O_RDONLY))==-1) /* open READ file */
my_error("Error opening ", source , FATAL);
if ((file2=creat(destination, S_IWRITE | S_IREAD)) <0)
my_error("Error opening ", destination, FATAL);
while ((n=read(file1, buf, BUFSIZE)) >0 )
if (write(file2, buf, n) !=n)
my_error("Write error on file ", destination, FATAL);
close(file1);
close(file2);
}
The program crashes when it is only 2 or 3 levels deep into the
recursion (ie 2 or 3 directory levels.) If I increase the stack size to
something like 10000 befre I run the program it works fine. My
question is, where does the memory come from when the program calls
copyfile()? Memory for variables comes from the stack, and
dynamically alocated memory comes from the heap? If so, is it really
necessary to increase the stack size for simple programs such as this?
In an attempt to figure out what happens with the stack during
recursive routines, I wrote the following program:
main(argc, argv)
int argc;
char *argv[];
{
foobar(1);
}
foobar(counter)
int counter;
{
printf("%d", counter);
if (counter<10000)
foobar(counter+1);
}
}
When I let it run, sometimes it crashes, and sometimes I get a requester
saying "Program_foo: Stack overflow". Is Lattice inserting this into
my code when it compiles? And why does it only work SOMETIMES??
Finally, can anyone recommend a good book for learning to program the
Amiga in C? I ordered "Programmers Guide to the Amiga" by Rob Peck
which I should get my hands on shortly. Any and all help much
appreciated!
-George
Technical Details
Message-ID:
<Dec.30.13.09.02.1989.28450@topaz.rutgers.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!uakari.primate.wisc.edu!uwm.edu!rutgers!aramis.rutgers.edu!topaz.rutgers.edu!armholdShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!uakari.primate.wisc.edu!uwm.edu!rutgers!aramis.rutgers.edu!topaz.rutgers.edu!armhold From: armhold@topaz.rutgers.edu (George Armhold) Newsgroups: comp.sys.amiga.tech Subject: Memory allocation Keywords: stack size, heap, Lattice 5.04 Message-ID: <Dec.30.13.09.02.1989.28450@topaz.rutgers.edu> Date: 30 Dec 89 18:09:07 GMT Organization: Rutgers Univ., New Brunswick, N.J. Lines: 76