ubuntu 使用gets函数编译警告不能运行

kuaildg 2012-02-11 02:18:40
各位大侠:
帮忙看看下面代码,通过ubuntu下的gcc编译出现警告之后,就退出编译。不知错在哪里,请各位大侠帮忙指点一下,谢谢!


#include <stdio.h>

int main()
{

char str[8];
gets(str);
puts(str);

return 0;
}


小弟文件名为gets.c 编译命令 gcc gets.c -o gets
...全文
555 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoeBlackzqq 2012-02-12
  • 打赏
  • 举报
回复
警告而已,没什么大不了的。
如果非要消除警告,就根据它的错误提示来改呀,应该不难吧。
kuaildg 2012-02-12
  • 打赏
  • 举报
回复
我明白了。之前有一次在别的程序中,由于没有生成二进制文件,所以有问题。现在生成了二进制文件,运行了一下,发现可以运行。而我还一直在纠结之前的那个没有生成二进制文件的问题。应该是之前的程序有问题吧。

此外,我也尝试用过fgets 取代gets,发现不再提示警告信息。
各位,谢谢了!
Lactoferrin 2012-02-12
  • 打赏
  • 举报
回复
警告而已,并没有说不能编译
你运行./gets
kuaildg 2012-02-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lactoferrin 的回复:]
你有什么警告
[/Quote]

kuaildg@kuaildg:/home/share/test$ gcc gets.c -o gets
/tmp/cct5eLYM.o: In function `main':
gets.c:(.text+0x29): warning: the `gets' function is dangerous and should not be used.
kuaildg@kuaildg:/home/share/test/$


通过命令查看,也生成了二进制文件

kuaildg@kuaildg:/home/share/test$ ls
gets gets.c
kuaildg@kuaildg:/home/share/test$


Lactoferrin 2012-02-12
  • 打赏
  • 举报
回复
你有什么警告
kuaildg 2012-02-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lactoferrin 的回复:]
gcc version 4.6.1
[/Quote]
这个我试了几次,我的ubuntu不能更新到4.6的。不知是不是ubuntu的版本问题。
更新的命令 sudo apt-get update
重装gcc :sudo apt-get install build-essential

还是不行,还是版本为4.4.3
Lactoferrin 2012-02-12
  • 打赏
  • 举报
回复
什么都没有


lenin@lenin-ubuntu:/tmp$ gcc gets.c -o gets
lenin@lenin-ubuntu:/tmp$ ./gets
Please input 8 letters:
ss
ss
lenin@lenin-ubuntu:/tmp$
kuaildg 2012-02-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lactoferrin 的回复:]
我是在ubuntu下编译,什么输出都没有
[/Quote]
那你把下面代码重新复制,编译一下,帮忙看看有没有问题。
   
#include <stdio.h>

int main()
{

char str[8];
printf("Please input 8 letters:\n");
gets(str);
puts(str);

return 0;
}


Lactoferrin 2012-02-12
  • 打赏
  • 举报
回复
gcc version 4.6.1
Lactoferrin 2012-02-12
  • 打赏
  • 举报
回复
我是在ubuntu下编译,什么输出都没有
kuaildg 2012-02-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lactoferrin 的回复:]
我这里没警告
[/Quote]
你确定是在 Ubuntu 下编译的么?
如果在其他的工具下,可能不会出现的。我当前的ubuntu的环境是ubuntu10.04 gcc版本是4.4.3.

gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
沭水河畔 2012-02-11
  • 打赏
  • 举报
回复
Linux下查man,说的很详细了,如下:
Reads characters from standard input until a newline is found. The characters up
to the newline are stored in BUF. The newline is discarded, and the buffer is ter‐
minated with a 0.

This is a _dangerous_ function, as it has no way of checking the amount of space
available in BUF. One of the attacks used by the Internet Worm of 1988 used this to
overrun a buffer allocated on the stack of the finger daemon and overwrite the
return address, causing the daemon to execute code downloaded into it over the con‐
nection.
昵称很不好取 2012-02-11
  • 打赏
  • 举报
回复
可能是怕越界造成危险
Lactoferrin 2012-02-11
  • 打赏
  • 举报
回复
我这里没警告
qq120848369 2012-02-11
  • 打赏
  • 举报
回复
++ 用fgets
justkk 2012-02-11
  • 打赏
  • 举报
回复
那就别用gets了,用fgets

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧