home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!mips!swrinde!cs.utexas.edu!sun-barr!news2me.ebay.sun.com!exodus.Eng.Sun.COM!peregrine!falk
- From: falk@peregrine.Sun.COM (Ed Falk)
- Newsgroups: comp.os.msdos.programmer
- Subject: Re: MY problem? ...or Borland's? (also on comp.lang.c)
- Message-ID: <l79sj6INNq8h@exodus.Eng.Sun.COM>
- Date: 28 Jul 92 07:08:22 GMT
- References: <1992Jul25.163852.19770@uwm.edu>
- Organization: Sun Microsystems, Mt. View, Ca.
- Lines: 57
- NNTP-Posting-Host: peregrine
-
- In article <1992Jul25.163852.19770@uwm.edu> rca@csd4.csd.uwm.edu (Robert C Allender) writes:
- > [character array mysteriously gets trashed]
- >
- >main() {
- > char drive, *c;
- > :
- > c = scrollbox(drive,61,10); <---- This gets a directory, etc.
- > :
- > list_dir(c); <-- by this point the array values are GONE.
-
- I strongly suspect that "c = scrollbox(...);" is the culprit. scrollbox()
- is returning a pointer to a character array, but where did this array
- come from? Does scrollbox look like this:?
-
- char *
- scrollbox(...)
- {
- char array[80] ;
-
- blahblah ;
-
- return array ;
- }
-
- If so, there's your problem. "array" is allocated on the fly from the
- stack when scrollbox() is entered. When scrollbox() becomes free again,
- "array" is returned to free space and a pointer to it is now a pointer
- to gibberish. The array contains the valid data for awhile, but the
- first function to come along and re-use that stack space will trash
- it. Instead, you should do:
-
- char *
- scrollbox(...)
- {
- static char array[80] ;
-
- blahblah ;
-
- return array ;
- }
-
- or
-
- char *
- scrollbox(...)
- {
- char *array = malloc(80) ;
-
- blahblah ;
-
- return array ;
- }
-
- -ed falk, sun microsystems
- sun!falk, falk@sun.com
- terrorist, cryptography, DES, drugs, cipher, secret, decode,
- DSS, FBI, NSA, CIA, NRO, SDI, communist, proletariat.
-