Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > gets() [newbie question]

gets() [newbie question]
Thread Tools
Junior Member
Join Date: Sep 2000
Location: Bedford, UK
Status: Offline
Reply With Quote
Apr 2, 2001, 05:59 AM
 
OK, this is probably a really stupid question but why doesn't gets() work in OS X? I'm new to programming and I'm currently working through the "C for Dummies" book as a start, and this is the first function that I can't get to work. The program compiles fine, but when I run it from the command line it ignores the gets() function and prints "warning: this program uses gets(), which is unsafe" instead.

So...
- why is it unsafe?
- is there to get it to work
- should I be using gets() at all?

Thanks
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 3, 2001, 09:20 AM
 
gets() is probably marked unsafe because it'll read until it's done. If the memory you're reading to is too little for gets(), it'll run over and try to write into other memory. fgets() is probably a better choice (or scanf() for other instances).
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Fresh-Faced Recruit
Join Date: Mar 2001
Status: Offline
Reply With Quote
Apr 3, 2001, 10:57 AM
 
Question 12.23
Why does everyone say not to use gets()?


--------------------------------------------------------------------------------

Unlike fgets(), gets() cannot be told the size of the buffer it's to read into, so it cannot be prevented from overflowing that buffer. As a general rule, always use fgets(). See question 7.1 for a code fragment illustrating the replacement of gets() with fgets().

References: Rationale Sec. 4.9.7.2
H&S Sec. 15.7 p. 356

RTFM: http://www.eskimo.com/~scs/C-faq/top.html

cu
Lars

     
Junior Member
Join Date: Sep 2000
Location: Bedford, UK
Status: Offline
Reply With Quote
Apr 4, 2001, 08:27 AM
 
Thanks you guys, that's helped a lot.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:51 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2