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() dangerous to know?

gets() dangerous to know?
Thread Tools
Mac Enthusiast
Join Date: Mar 2001
Location: NC, USA
Status: Offline
Reply With Quote
Apr 11, 2001, 06:13 PM
 
Hi all,
Since installing OSX, I've been thinking about my old unix days in college. Knowing that OSX programmes are written in objective C, I decided to learn C and then work my way up to objective C once some good books were out. However, I've come upon my first stumbling block. My book, "C for Dummies vol.1", teaches the gets() function as a way to get keyboard input. So, I coded the exercise programme and compiled it with no problem. However, when I run it from the command line I get: "This programme uses the gets() function which is dangerous" (or something like that.) Why is gets() dangerous? Should I substitute scanf(), or is there some other function I should be using instead of gets()? Your help will be much appreciated by a "programmer in training". Cheers,
Derrick

------------------
10:00 conocí a Apple. 10:30 trabajé con Apple. Apple Computer®
Satellite deployment by:
Ace Moving Co.
     
Mac Elite
Join Date: Mar 2001
Location: Provo, UT
Status: Offline
Reply With Quote
Apr 11, 2001, 06:23 PM
 
gets() is indeed dangerous and rarely works as advertized. In general you should write your own equivalent. (Speaking as one who learned this the hard way years ago and who didn't have a compiler who provided this helpful warning)

Why a book would encourage using this function is beyond me.

You should also, as you use stdio, read carefully the requirements. I had a class a few weeks ago with a subtle bug because of the limitations stdio had switching between reading and writing without flushing. I've noticed that a lot of my bugs end up being due to making assumptions about these things. . .

While it's undoubtedly overkill for what you are doing, anyone else using stdio might find the book The Standard C Library helpful. It's saved my butt a few times. It is also interesting seeing how the standard function libraries were written.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 11, 2001, 06:57 PM
 
gets() is evil because it can "overflow the buffer." This means that it writes past the memory allocated to the buffer (the string you're storing the value of gets() to).

Remember next time to search the forum for the question you're asking (Sorry, I know it's a pain ). The question was asked by Lars-1 a week or two ago.
http://forums.macnn.com/cgi-bin/Foru...ML/000705.html
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
MacmanX  (op)
Mac Enthusiast
Join Date: Mar 2001
Location: NC, USA
Status: Offline
Reply With Quote
Apr 11, 2001, 07:24 PM
 
Thanks!
I will continue on my path to C enlightenment.

Parallax,
I hadn't noticed the search option before. Thanks. That will save me a lot of time digging through posts.

------------------
10:00 conocí a Apple. 10:30 trabajé con Apple. Apple Computer®
Satellite deployment by:
Ace Moving Co.
     
   
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 03:03 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