Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Here is a sample code
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
int t, i;
char text[100][80];

for (t=0; t<100; t++){
cout << t << "; ";
gets(text[t]);
if(!text[t][0]) break;
}
for (i=0; i<t; i++)
cout << text << '\n';
return 0;
}

When I compile this code, although it compiles correctly, xCode gives me a message at the start of the executable file "warning: this program uses gets(), which is unsafe.". Why? Although I can run the program flawlessly, that warning bothers me, because don't know if this command will cause problems when I try to write other programs.

Oh, and how can I create a box that contains code in this forums? (you know, just like the box of a quote, but with code).
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,424
1,065
Bergen, Norway
From C/C++ Programmers Reference: "There is no way to limit the number of characters that gets() will read, which means that the array pointed to by str could be overrun. Thus, this function is inherently dangerous. Its use should be limited to sample programs or utilities for your own use. It should not be used for production code."
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
ok thanks! This "programmer reference"... What is it? Just another book?

Sorry if my question sounds newbish...
 

broken_keyboard

macrumors 65816
Apr 19, 2004
1,144
0
Secret Moon base
You can tell without the book. Look at the function call, you are passing an array without any indicator of it's size. How can the function possibly know how many chars are pointed to? There is no way.

The man page for gets recommends using fgets instead...
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
GeeYouEye said:
WAY off topic, but...

soulstorm: your signature is inaccurate: iPods have 32 MB of RAM. ;)
Didn't know that my iPod had RAM. Come to think of it, how it kept in its memory some songs to be played? Should have noticed it. Anyway, I fixed my sig. Thanks ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.