Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's so bad about system()? [closed]

Given that you're building a Windows - exclusive program, why are calls to system() so hated? Ever since I started programming in C++, "don't touch system" was something I've heard an awful lot. I've obeyed that principle docilely but what's so bad about it?

like image 668
Nikola Dimitroff Avatar asked Oct 19 '13 16:10

Nikola Dimitroff


2 Answers

What you put inside the system call is dependent on the OS you will run the program on. So it's completely non-portable. Also, many things can go wrong, as you're just throwing a command into the void, and hope that everything will go fine. Error handling (for example - the program you want to run does not exist, or PATH is not set correctly, or you don't have permissions, etc.) is very hard or even impossible.

Beside this, as noted in the comments most of the time (really "most", like 99.999%) there is no need to use system.

like image 189
BartoszKP Avatar answered Sep 28 '22 17:09

BartoszKP


std::system() requires that you trust the command to do what you want. You have no way of verifying whether this assumption is justified. For example an unusual value for the PATH environment variable may cause a different program to be executed than in the standard setup.

The behaviour of a call to std::system() is system dependant. Even if the requirement at the moment is, that the application does not need to run on other operating system, this requirement can change. Considering that std::system() is often used for purposes that can be achieved in a more portable manner (especially by beginners), introducing platform dependance here is not required.

like image 37
Oswald Avatar answered Sep 28 '22 17:09

Oswald