Article View: comp.sys.amiga.tech
Article #13342Re: Memory allocation
From: Bill Squier
Date: Mon, 01 Jan 1990 23:00
Date: Mon, 01 Jan 1990 23:00
54 lines
2425 bytes
2425 bytes
In article <Dec.30.13.09.02.1989.28450@topaz.rutgers.edu>
armhold@topaz.rutgers.edu (George Armhold) writes:
]
]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.
] [stuff deleted]
]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 */
]
] [ rest of code deleted... ]
]
]The program crashes when it is only 2 or 3 levels deep into the
]recursion (ie 2 or 3 directory levels.)
] [ more creative deleting... ]
As was already mentioned, auto class variables created within functions
are always allocated off the stack. The two previous suggestions for
fixing this are valid, but it's my personal preference to declare
buffers (such as your buf[1024]) as external variables. To do this, you
would declare it outside all functions. I prefer to put my global
variables near the functions that use them. For example:
char buf[1024];
void copyfile(source,dest) /* ... */
This avoids a new copy of ``buf'' being generated each time you call
copyfile().
]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
The book you mention above is a good one, but you may also want to try
"Programming in C" by Stephen Kochan, now in its second edition, I
believe. This book will also teach you more about 'C' in general, such
as recursion, and how different types of variables are stored.
--
Bill Squier - Stevens Inst. of Tech | // "Only Amiga makes it possible"
Bitnet: u93_wsquier@stevens | \X/
Internet: u93_wsquier@vaxc.stevens-tech.edu
Temporary Inet (Please use until Jan. 13): ...uunet!tronsbox!dsoft!groo
Technical Details
Message-ID:
<434@dsoft.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!asuvax!mcdphx!mcdchg!ddsw1!tronsbox!dsoft!grooReferences:
<Dec.30.13.09.02.1989.28450@topaz.rutgers.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!cs.utexas.edu!asuvax!mcdphx!mcdchg!ddsw1!tronsbox!dsoft!groo From: groo@dsoft.UUCP (Bill Squier) Newsgroups: comp.sys.amiga.tech Subject: Re: Memory allocation Keywords: stack size, heap, Lattice 5.04 Message-ID: <434@dsoft.UUCP> Date: 1 Jan 90 23:00:18 GMT References: <Dec.30.13.09.02.1989.28450@topaz.rutgers.edu> Lines: 53