system() --- the C function from hell?

David McIntyre mcintyrd at cs.rpi.edu
Sun Apr 30 21:45:44 AEST 1989


In article <1827 at uop.edu> jeff at uop.edu (Jeff Ferguson) writes:
>	The system() call seems to wreak havoc with integer variables.
>I have the following code:
>
>int lowp, hip;
>...
>scanf("%d", &hip);
>scanf("%d", &lowp);
>sprintf(str, "/bin/mkdir directory");
>system(str);
>...
>
>I printed the values of LOWP and HIP before the system() call, and
>everthing was pee-chee (like the folder).  I print them out after
>the call and the values have been blown to high Heaven: before they
>were both 3, while after system(str) they are in the tens of millions.

I would suggest that str is a (char *) variable that has not
had any space allocated for it, and it is overwriting the
two integers somehow.  You should either make str something like

char str[20];

or malloc it, something like this:

	str = (char *)malloc(20*sizeof(char));


Dave "mr question" McIntyre     |      "....say you're thinking about a plate
mcintyre at turing.cs.rpi.edu      |       of shrimp.....and someone says to 
office : 518-276-8633		|	you `plate,' or `shrimp'......"
home   : 518-271-6664		|



More information about the Comp.unix.questions mailing list