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