home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / msdos / programm / 8136 < prev    next >
Encoding:
Text File  |  1992-07-28  |  1.8 KB  |  69 lines

  1. 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
  2. From: falk@peregrine.Sun.COM (Ed Falk)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: MY problem? ...or Borland's? (also on comp.lang.c)
  5. Message-ID: <l79sj6INNq8h@exodus.Eng.Sun.COM>
  6. Date: 28 Jul 92 07:08:22 GMT
  7. References: <1992Jul25.163852.19770@uwm.edu>
  8. Organization: Sun Microsystems, Mt. View, Ca.
  9. Lines: 57
  10. NNTP-Posting-Host: peregrine
  11.  
  12. In article <1992Jul25.163852.19770@uwm.edu> rca@csd4.csd.uwm.edu (Robert C Allender) writes:
  13. > [character array mysteriously gets trashed]
  14. >
  15. >main() {
  16. >  char drive, *c;
  17. >    :
  18. >  c = scrollbox(drive,61,10);  <---- This gets a directory, etc. 
  19. >    :
  20. >  list_dir(c);                   <-- by this point the array values are GONE.
  21.  
  22. I strongly suspect that "c = scrollbox(...);" is the culprit.  scrollbox()
  23. is returning a pointer to a character array, but where did this array
  24. come from?  Does scrollbox look like this:?
  25.  
  26.     char *
  27.     scrollbox(...)
  28.     {
  29.         char    array[80] ;
  30.  
  31.         blahblah ;
  32.  
  33.         return array ;
  34.     }
  35.  
  36. If so, there's your problem.  "array" is allocated on the fly from the
  37. stack when scrollbox() is entered.  When scrollbox() becomes free again,
  38. "array" is returned to free space and a pointer to it is now a pointer
  39. to gibberish.  The array contains the valid data for awhile, but the
  40. first function to come along and re-use that stack space will trash
  41. it.  Instead, you should do:
  42.  
  43.     char *
  44.     scrollbox(...)
  45.     {
  46.         static char array[80] ;
  47.  
  48.         blahblah ;
  49.  
  50.         return array ;
  51.     }
  52.  
  53. or
  54.  
  55.     char *
  56.     scrollbox(...)
  57.     {
  58.         char *array = malloc(80) ;
  59.  
  60.         blahblah ;
  61.  
  62.         return array ;
  63.     }
  64.  
  65.         -ed falk, sun microsystems
  66.          sun!falk, falk@sun.com
  67.     terrorist, cryptography, DES, drugs, cipher, secret, decode,
  68.     DSS, FBI, NSA, CIA, NRO, SDI, communist, proletariat.
  69.