Why should the system() function be avoided in C and C++?
Asked Answered
D

3

22

I have seen a lot of people on forums telling to avoid the system() function, like system("cls"). I don't understand why.

Please tell me why I should avoid this function. And also, as clrscr() doesn't work with CodeBlocks, what are other ways to clear screen without using the system() function?

Doublecross answered 11/11, 2013 at 18:40 Comment(8)
It's system, not System, and clrscr isn't a standard function. There is no standard way to clear it, but I'll bet it's not the IDE that clrscr doesn't work with.Mariquilla
It probably is system (lower-case letters). And the cls command is operating-system specific (does not exist on Linux), and could have been removed or renamed by the sysadmin...Decibel
it's not portable; what works on windows might not work on linux or mac.Recurrent
You might be interested by ncurses at least on Linux.Decibel
system is a call that accepts any kind of unauthenticated shellcode, and it invokes a command interpreter that you usually do not want. Insofar it's extra overhead for adding a possible security exploit to your program.Princessprinceton
It can cause a security vulnerability: securecoding.cert.org/confluence/display/seccode/…Lepidosiren
Why do you want to clear the screen? There might be useful information there.Rather
You should avoid system-dependent code in code intended to be a portable. But there's no reason to avoid system in general. The situation for system is much the same as for using the arguments of main: it's system dependent, so you'd better know what you're doing, but it would be silly to avoid it in general.Discard
M
33

There are multiple problems here:

  • First of all, system() as a function is cross-platform and available not just on Windows or Linux. However, the actual programs being called might be platform dependant. For example, you can use system() to create a directory: system("md Temp"). This will only work on Windows, as Linux doesn't know a command called md. For Linux it would have to be system("mkdir Temp"). This goes on, so you'd need a custom solution for each and every platform.
  • This will always spawn a child process that's then executing something. This will in general be slower than some inlined code, e.g. the command or program has to be loaded, has load it's own dependencies, then it has to be executed etc. which is usually a lot more work.

If you're just doing some quick testing on one platform, using system() is perfectly fine, but you shouldn't use it in production environments, unless you really have to. For example, you could allow the user to set an external program that is then executed. For something like this system() is perfectly fine.

Movable answered 11/11, 2013 at 18:50 Comment(3)
In addition: If you have a dependency on a certain program (eg for displaying results) a system call might be preferable.Radicel
Upvoted because you explain that it depends on context. It is the same with your example to let the system set external programs. Could be ok sometimes. Sometimes cross paltform is not an issue either.Overton
also it is blocking system call.Colonialism
P
4

There is an answer about system() usage. And there is no standard C++ way to clear console window. For Windows platform you can use such code:

void clear() 
{
    COORD startPos  = { 0, 0 };
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO screen;
    DWORD written;

    GetConsoleScreenBufferInfo(hConsole, &screen);
    FillConsoleOutputCharacterA(hConsole, ' ', screen.dwSize.X * screen.dwSize.Y, startPos, &written);
    FillConsoleOutputAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, screen.dwSize.X * screen.dwSize.Y, startPos, &written);
    SetConsoleCursorPosition(hConsole, startPos);
}

And for linux, never tried, but found the way:

#include <curses.h>
erase();
Pruritus answered 15/6, 2016 at 10:52 Comment(0)
G
1

You should avoid system call because

  • Those calls are not portable, they might not work on other platforms.
  • Those calls are expensive to call, why would you let your resources got eaten?

How to clear your console? You can use std::cout << std::string(50, '\n');

Goatskin answered 11/11, 2013 at 18:50 Comment(9)
"You need to include some headers to use such functions." - That's actually something you'll always have, no matter which library you're using. Also just printing lots of line breaks won't necessarily clear your screen properly. For example, under Windows nowadays most console windows have a buffer of about 300 lines.Movable
I already removed last bullet, that's completely true what you say.Goatskin
cout << string(300, '\n'); checkmate @MovableRecurrent
Wouldn't you also want to flush to be sure?Overton
@stellarossa: That will create 300 empty lines in your scrollback or when redirecting output to a file. Still not really elegant to use. ;)Movable
... and not every console has less than 50 rowsNorthrup
@Mario, also console has scrollbar, and even if output enough lines, you'll get to the bottom instead of the top. And I configured my console to have 9999 lines (maximum possible on windows).Trenchant
@Trenchant Correct, but that's even the case when invoking system commands like clear. If you scroll up, stuff will appear again, there's no real guarantee for this to work (although it might work in one environment, it won't necessarily work in the other).Movable
@Mario, nope. On windows cls command really clears console and scrolls to top.Trenchant

© 2022 - 2024 — McMap. All rights reserved.