home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11496 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  2.0 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!mips!pacbell.com!att!rutgers!bagate!cbmvax!cbmehq!cbmger!edohwg!heinz
  2. From: heinz@edohwg.UUCP (Heinz Wrobel)
  3. Newsgroups: comp.lang.c
  4. Subject: Re:  Bugs with sscanf()?
  5. Message-ID: <heinz.02l4@edohwg.UUCP>
  6. Date: 21 Jul 92 20:38:33 GMT
  7. References: <6JUL199209135459@ariel.lerc.nasa.gov> <6JUL199210231568@ariel.lerc.nasa.gov>
  8. Organization: Edotronik GmbH
  9. Lines: 39
  10.  
  11. In article <6JUL199210231568@ariel.lerc.nasa.gov> smneyln@ariel.lerc.nasa.gov (Michael Neylon) writes:
  12.  
  13. [code deleted]
  14.  
  15. >Someone mentioned that the buffer may be erased or partial deleted after
  16. >returning from the field.  However, using a debugging tool, I saw that the
  17. >field is still perfectly intact just before execution of the scanning.
  18.  
  19. You _almost_ got the solution to your problem yourself.
  20. The buffer "a[]" is an auto buffer. auto stuff is allocated on the current
  21. functions stack. What the "return a" does is to pass a stack address to
  22. main that is no longer valid! It is invalid because you left the
  23. function string() which makes its stack frame invalid. The _invalid_ stack
  24. contents still look good before you do the scanning but the very call to
  25. the scan function destroys the stack you _had_ been using as buffer in
  26. string().
  27.  
  28. You can either declare a[] as static (IMHO Argghhh! for some reasons) or
  29. put a[] into main and pass a pointer to it to string() (IMHO better). That
  30. way you call string with a _valid_ buffer pointer and get valid buffer
  31. contents back as they "belong" to the main() stack frame.
  32.  
  33. Check out the FAQ on pointers and auto stuff.
  34.  
  35. Hope this helps. :-)
  36.  
  37.  
  38. >Mike Neylon     aka Masem the Great and Almighty Thermo GOD!
  39.  
  40. PS: How about ANSI or C++ ;-) :-)
  41.  
  42.  
  43. --
  44. Heinz Wrobel, Edotronik GmbH (ECG018)
  45. FAX +49 89 850 51 25 / TEL +49 89 850 25 20 (HOME!&VOICE, sometimes...)
  46. Path: cbmehq!cbmger!edohwg!heinz@cbmvax.commodore.com
  47. "It's good to have a mouse, it's faster if you can do without one..."
  48. "He who doesn't develop with an A2024 doesn't know about font independent
  49.  user interfaces..."
  50.