在C和C++里,要尽量避免使用 system("pause")
来源:互联网 发布:vscode react 编辑:程序博客网 时间:2024/06/18 18:18
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://amigniox.blogbus.com/logs/47563613.html
system("pause")
I've never understood why system("PAUSE") is so popular. Sure it will pause aprogram before it exits. This pause is very useful when yourIDE won't waitas you test a program and as soon as the program finished the window closes taking all your data with it.
But using system("PAUSE") islike burning your furniture for heat when you have a perfectly good thermostat on the wall.
Many people, instructors included, for some inexplicable reason think that making a call to the operating system and running a system command totemporarily halt a program is a good thing. Where they get thisidea is beyond me. Reasons:
It's not portable. This works only onsystems that havethe PAUSE commandat the system level, like DOS or Windows. But notLinux and most others...
It's a very expensive and resource heavy function call.
It's like using a bulldozerto open your front door. It works, but the key is cleaner, easier,cheaper. What system() doesis:suspend your program
call the operating system
open an operating system shell (relaunches the O/S in asub-process)
the O/S must now find the PAUSE command
allocate the memory to execute the command
execute the command and wait for a keystroke
deallocate the memory
exit the OS
resume your program
There are much cleaner ways included in the language itself thatmake all this unnessesary.
You must include a header you probably don'tneed: stdlib.h or cstdlib
It's a bad habit you'll have to break eventually anyway.
Instead, use the functions that are defined natively in C/C++already. So what is it you're trying to do? Wait for a key to bepressed? Fine -- that's called input.So in C, use getchar() instead.In C++, how about cin.get()?All you have to do is press RETURN andyour program continues.
Note: the origin of the article isn't specified.
- 在C和C++里,要尽量避免使用 system("pause")
- C++第1天:在C和C++里,要尽量避免使用 system("pause")
- C system("PAUSE") 小议
- C语言中system("pause")是什么作用?
- system("pause")在使用freopen时失效
- 【C语言】【unix c】pause(2)使用
- system("pause") 和getchar()
- C/C++内存泄露--工程中如何尽量避免
- 尽量避免在SQL语句中使用OR
- 尽量避免在SQL语句中使用LIKE
- 尽量避免在Java中使用常量类
- 在FPGA开发中尽量避免全局复位的使用?
- Oracle 尽量避免在SQL语句中使用 OR
- Oracle 尽量避免在 SQL语句中使用 LIKE
- 在FPGA开发中尽量避免全局复位的使用
- [转贴]部分C语言库函数。为了避免重复造车轮,请尽量使用标准库提供的函数
- [转贴]部分C语言库函数。为了避免重复造车轮,请尽量使用标准库提供的函数
- 在C++Builder里使用AGG库
- Fedora的帮助网页--详细的哟
- 程序出现Access violation - code c0000005 (first chance)的解决方法
- PostgreSQL与MySQL命令比较
- 沟通管理技巧漫谈
- 开发人员、程序员与计算机科学家三者之间的区别
- 在C和C++里,要尽量避免使用 system("pause")
- 当构造函数定义在private或者protected里时
- Tomcat服务器环境搭建
- ForeFrontTMG 和 Wireshark
- Ubuntu 12.04 iNode Client找不到库libjpeg和libtiff的解决方法
- PSN下载加速相关程序教程(PS3.ProxyServer和PSN DM)
- ZipArchieve方法时的编码问题
- Windows作为Web服务器的安全设置攻略
- Sublime Text 2搭建Java开发环境及输出编码问题解决