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?
The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib. h> or <cstdlib> should be included to call this function.
System() is vulnerable as the command used can be replaced. To avoid it we can use library functions like fork execl, execv, execle, execve, execlp, execvp . Save this answer.
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.
A system call can be defined as a request to the operating system to do something on behalf of the program. During the execution of a system call, the mode is change from user mode to kernel mode (or system mode) to allow the execution of the system call.
There are multiple problems here:
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With