Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 18th, 2005, 4:23 PM   #1
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
Why shouldn't we use System();

Quote:
Originally Posted by So many people
system("PAUSE");
Well this is not meant to be criticism , but i dont think it is that good to recommend system() at all, since this functions has much more cons than pros... well,of course, i am no expert but here are some things i have on my mind, and because of them i would never use system, if there is an alternative way to do the job, though it unfortunately is a standard function...

1) its commands are totally platform dependent. So you wont be able to use your sources on other OSs

2)a shell is started which could do a lot.. for example stop the programm, or do smth else (depends on the kind of shell)

3)Everytime you use system u start a shell, this means that code isnt fast at all , and since the shell will be exited after the system-independent function call , you are not able to change environment vars, because the changes will be undone when exiting the shell...

4) a real important thing is the risk ,while programming cgi-codes with system ... if some user for example can make some input in a stream or string and you call system(input.c_str()) or system(input.str().c_str()) he will be able to use meta signs ( which every shell has) to execute it with other permissions or something else..
( something like "; command_to_do;other_command i could imagine on *nixes
but dont say u will parse the input, in my opinion
>>Instead of checking input for shell characters, stop using a shell<<
if the system()-shell is started with root permissions and it fails to terminate in a cgi programm this would be another bad thing too.

5) U dont need a shell in most of the cases ( indeed,i know that there are some)one example where you dont need it
is system("Pause");
...


6) In most of the cases you can read system() the job it does isnt worth the costs of starting and exiting a shell!.

i hope nobody is mad about this post... well i just want to list the facts for people who are interested or maybe start programming in c or c++. They cant know that, so its a fine thing to start coding without such things.....
well, thats my opinion...

so far...
__________________
of all the things he has lost, i think he misses his mind the most

typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza;
Shapeless is offline   Reply With Quote
Old Jul 18th, 2005, 7:31 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
I would agree to stay clear of system() when alternatives are available in final, completed projects. I have no objection to it when writing small test programs for yourself just to find out how something works -- if I'm the only one going to see the program, what difference does it make? If you're going to turn the code in to your instructor at school then you should find out from him/her about coding standards.
Ancient Dragon is offline   Reply With Quote
Old Jul 18th, 2005, 7:57 PM   #3
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
well, system is a standart functions, thats not the catch...
I do belive that its always good to write source code as good as possible, in large projects, libs , in apps or just in test modules or while learning a language.
Using non-standart or such... "dangerous" or specific functions should be avoided from the beginning, otherwise it could become a bad habit. if there is an alternative way there is no reason to use it...
and in case of a little test code or module for anything... i think I would be really angry to realize that my code doesnt run on another machine because of such a little thing...

and another thing.. i was talking about recommending or telling about it , I know that the default (hello world )project on DevCpp uses System. and of course i can understand that thats not an important thing while you are learing the basics...
but its never wrong to know ( even in the early times ) that there are alternatives...

but I have to admit that you are right, the dangers i told about are not important ( wont appear) in little "hello world" sources or smth similar.
I wasnt just talking about System("Pause"); i was talking about it in gerneral, ( for example in cgi apps/libs)

so far
__________________
of all the things he has lost, i think he misses his mind the most

typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza;
Shapeless is offline   Reply With Quote
Old Jul 18th, 2005, 9:29 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 550
Rep Power: 4 Ancient Dragon is on a distinguished road
I don't really have any object to what you are saying -- except maybe that it is "dangerous". College and university profs often teach non-standard C with ancient compilers. Take for example use of Turbo C compiler in the classrooms. That is a horrible way to teach anybody how to write c programs because most 32-bit compilers today don't support any of the old 16-bit Turbo C functions. Then students complain biterly because their old Turbo C programs can not be compiled by those "stupid" Dev-C++ and similar compilers. Turbo C should be permanently banned from all colleges and universities around the world! Any prof teaching it should have his toung cut out!

system() and other similar functions are really intended to execute other programs and scripts and should not be used instead of standard C or C++ library functions or os api functions.
Ancient Dragon is offline   Reply With Quote
Old Jul 19th, 2005, 3:59 AM   #5
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
Well:
full ack!
i know what you are talking about.
unfortunately a lot of profs from many universities teach the way you
__________________
of all the things he has lost, i think he misses his mind the most

typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza;
Shapeless is offline   Reply With Quote
Old Jul 19th, 2005, 5:29 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Shapeless
well, system is a standart functions, thats not the catch...
Yes, system() is standard, but the specification in the standards for what it does are quite loose (lots of aspects of implementation defined behaviour). That underlies several of the gotchas you mentioned earlier.

system("pause") is, however, non-standard (or, more accurately, there is no guarantee that system("pause") will do anything). It just happens to work under Microsoft operating systems, but it doesn't under others.

Quote:
Originally Posted by Shapeless
I do belive that its always good to write source code as good as possible, in large projects, libs , in apps or just in test modules or while learning a language.
Sure.
Quote:
Originally Posted by Shapeless
Using non-standart or such... "dangerous" or specific functions should be avoided from the beginning, otherwise it could become a bad habit. if there is an alternative way there is no reason to use it...
The problem with the requirement to "wait until the next key is hit" is that there is no standard means of doing it.

C and C++ are actually fairly limited on their own. In practice, it is often mandatory to use system or compiler specific functions. If one could not do that, one would be limited in many ways: no GUI, no direct keyboard input, no threads, no networking, no directory listings, etc etc.

Accordingly, a better rule is to be portable where possible, but accept the need to do non-portable things at times in the interests of getting things done.

Quote:
Originally Posted by Shapeless
and in case of a little test code or module for anything... i think I would be really angry to realize that my code doesnt run on another machine because of such a little thing...
Then you will be really angry if you ever seek to do programming in the real world.

One part of professional software engineering is to be explicit when you do things non-portably. A lot of effort goes into defining higher level functionality which (for example) may be implemented using one approach on system A (eg a windows box) but is implemented using a different approach on system B (eg a unix box).
grumpy is offline   Reply With Quote
Old Jul 19th, 2005, 5:42 AM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
The problem with the requirement to "wait until the next key is hit" is that there is no standard means of doing it.
Sure there is - std::cin.get()
Cerulean is offline   Reply With Quote
Old Jul 19th, 2005, 6:10 AM   #8
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
thanks ,Cerulean
anotherone is getchar();

and even if u dont feel like usuing a standart function use getch(); from conio.h instread of system


Quote:
Then you will be really angry if you ever seek to do programming in the real world.
well, not always.. at my last job i had to use some kind of "selfwritten" lib-cgi which does run on linuxes and windows ....
my current job uses the platform (well designed ) gui lib wxWidgets and the whole project is going later run on linux and windows.

i know that there are a lot of situations the customer just wants a specific solution. and ,of course, then platform independency isnt interesting.
but i spend my time with writing little libs or smth and so i want to be as independent as possible.
and if someone begins programming i think its good to learn the independent things first ...and later the os-dependent.
but if there is no independent way , then use the os-apis ..
__________________
of all the things he has lost, i think he misses his mind the most

typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza;
Shapeless is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC