views:

420

answers:

3

Hello,

I am trying to pass in arguments to the a parent file that is supposed to create a child process for each pair of arguments. The child processes will add up each pair and return their sum to the parent process. If there is an odd number of arguments passed in, I add a 0 to the end of the argv array to make it even. This keeps happening until all the arguments have been added up, and their sum is printed at the end.

Everything is working fine except when I pass in an even amount of arguments. A child process will successfully add the first two arguments and return them, but then when the parent does a sprintf, (line 58) there is always a segmentation fault.

Here is the code for both the parent and child processes (I am using pastebin so it doesn't look very cluttered here. They will expire in one day, so I could re-post them if needed):

The second file has to be called worker when it is compiled in order to be run by the first file. I am using gcc on Ubuntu 9.10

Here are a few examples on how to run the program:

gcc -o parent parent.c
gcc -o worker worker.c
./parent 1 2 3 4

(The above example will end with a segmentation fault like I explained above). The answer should be 10 because (1 + 2) + (3 + 4) = 10.

This one below will work fine though with an odd number of arguments being passed in:

./parent 1 2 3

The answer to this one should be 6.

Any help would be greatly appreciated!

+3  A: 

Overwriting the argument list is, at best, a serious perversion, and at worst a guaranteed fault. That's what you're doing in your sprintf call - the first argument you pass it is one of the strings passed to main(). Why are you doing that?

[edit] I see you've even got a comment to that effect. Well, I don't know what you think is supposed to happen when you do that; if you explain your thinking then somebody may be able to explain where you've gotten confused. You can't add new stuff to the "argv" array. It makes no sense. That's something allocated by the system when launching your process, and you should basically treat it as being read-only (unless you really know what you're doing).

Pointy
Thank you for your reply. So I should copy all of the new sums to a separate integer array then? I thought I could just keep my for loop going by adding all the new sums at the end and incrementing argc
Dave
Maybe it's a homegrown `setproctitle`! :-P
Chris Jester-Young
@Dave what is it that you expect will happen when you add strings to the argument list? In other words, what's the goal? What made you think that that was a good idea? (Not making fun of you; I'm trying to get to the bottom of what it is you're trying to accomplish.)
Pointy
I wanted to keep the loop going by just adding all of the new sums to the argv array. In other words, I thought it would be simple because I wouldn't have to convert the integers returned back from the child process to strings and then add them to a new char array.
Dave
A: 
        argc++;
        sprintf(argv[argc-1],"%d",tmp);

you are getting out of the array boundaries here. btw, what is use to write to argv ?

Andrey
Hello, the code you put write there is what I am using to add values to the argv array.
Dave
The beauty of C is that it's just memory. The argv array isn't an array like in PHP or javascript, where you can dynamically add items and it understands. All the [] operator does is access a piece of memory at a given offset. Since the size of the array is fixed, you're trying to access a piece of memory outside its control. This could cause any number of odd behaviors (from a segfault to just weird heisenbugs). In short, don't do it!! :)
jdizzle
Haha thank you!
Dave
+2  A: 
Alok
Thank you, that makes a lot of sense!
Dave