Problem with simple piece of code

This is a discussion on Problem with simple piece of code within the C++ Programming forums, part of the General Programming Boards category; I'm having a bit of trouble with an excercise for C++ (for reference, I use SSH to connect to my ...

Problem with simple piece of code

I'm having a bit of trouble with an excercise for C++ (for reference, I use SSH to connect to my University and code using Unix with g++ compiler).

This simple piece of code (which after much stress over being unable to get the function to work on my own) I copied directly from a sample excercise and it still gives me the following when I try to excecute it: "Segmentation Fault". Can somebody tell me - what's wrong here?

Ah, I did not know this error would appear if I did not pass enough arguments. I was at first using this code (below), but I was having a problem with writing <stdlib.h> as <stdlib> and in checking what was wrong I tried replacing "argc - 1" as size of arrays with actual numbers to see if that was a problem. After fixing the real problem I ended up still having problem because I never tried reverting back to the original code.

Segmentation fault is a signal that your linux operating system sends to your program when it detects your program accessing or overwriting an area of memory that it shouldn't. The signal causes your program to abort and (optionally) report the error message you're seeing.

There are two potential contributors to this in your code;

1) marks is an array of length 5 (with values marks[0] through to marks[4]). Your code is modifying marks[5] which does not exist (due to the i < 6 end condition for the loop).

2) Your loop is evaluating atoi(argv[i+1]) (for i = 0 to 5, inclusive) without checking the value of argc. If you have not run your code with 6 command line arguments, these argv[i]'s will not exist.

Formally, because of these things, your code is said to exhibit undefined behaviour (the C++ standard says nothing about segmentation faults). According to the standard, anything is allowed to happen with code that exhibits undefined behaviour: no compiler diagnostics are required, any symptom can occur.

Unrelated to your problem, but something to be aware of;

1) main() returns int, not void. Any book that gives examples of main() returning void is implicitly of low quality and reliability.

2) instead of <stdlib.h>, it is better to use <cstdlib> in C++. <stdlib.h> is not disallowed, but is discouraged (deprecated) by the C++ standard. The main practical difference between <cstdlib> and <stdlib.h> is that functions/symbols are placed within namespace std: your code has a "using namespace std;" directive, so will be - mostly - unaffected by that difference.

Sorry, by "work" I meant the code will do what I want it to (I generally try to ensure I can hand in the excercise and get the marks before I go about seeing how it can be done better). However, thanks for the suggestion, I will look up dynamic arrays for future.

Though I have to say, the quality and speed of receiving answers is excellent so thank you all for spending your time to help people like me.