srcs
srcs

Reputation: 67

program appears to continue running after exit (0)

At runtime, the program takes one or more arguments from the command line, each of which is the name of files to open.

argc == the number of filenames.

argv[0] == the program's name.

argv[n] , where n is an integer == a given filename, according to the order in which they are passed from the command line.

next_file advances to the next file to be edited. it increments n, and exits if n is greater than argc; since that would mean the last file had already been reached. otherwise, it calls file_handler.

file_handler takes a stream object [file], a string [name], and a boolean variable [open]. if open is 0, it closes the current file. if open is 1, it opens [name] with [file].

when next_file is called, the expected behavior is that it will close the currently open file, advance [n] by one, and then open the next file. it should exit before attempting to open a file that doesn't exist.

the function works normally until the last file is reached, at which point a debug assertion is thrown.

void next_file ( int & n , int argc , char *argv [] , fstream & file )
{
    n++;
    if ( n > argc )
        exit ( 0 );
    file_handler ( file , argv [n - 1] , 0);
    file_handler ( file , argv [n] , 1 ); //this appears to be the cause of the assertion failure
}

void file_handler ( fstream & file , string name , bool open )
{
    if ( open == 0 )
    {
        file.close ();
        file.clear ();
        return;
    }
    in.open ( name , ios::in | ios::out | ios::binary );
    if ( !in.is_open () )
    {
        cout << "\n Failed opening " << name << "\n\n";
        exit (0);
    }
}

Upvotes: 0

Views: 225

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145429

For the main arguments, the value of argv[argc] is guaranteed to be ​0​.

Then, with n == argc,

file_handler ( file , argv [n] , 1 )

… is equivalent to

file_handler ( file , 0 , 1 )

… where the 0 is used to initialize a std::string formal argument, which is rather ungood.

Upvotes: 0

Sam Varshavchik
Sam Varshavchik

Reputation: 118425

You running off the end of the array.

As you know, when an array has n elements, they are values array[0] through array[n-1].

n++;
if ( n > argc )
    exit ( 0 );

At this point, the highest possible value for n is argc, because if n > argc, exit(0) gets called. But when n is equal to argc, this will proceed. Therefore:

file_handler ( file , argv [n] , 1 ); //this appears to be the cause of the assertion failure

Of course this will be the cause of the assertion. The highest value of n here will be argc, as explained above.

And, of course, argv[argc] does not exist. There are argc values in argv, therefore the last one will be argv[argc-1].

In actuality, the way that argv argument to main() is set up, argv[argc] will return a nullptr. This parameter to file_handler() is a std::string, and the attempt to construct a std::string from a nullptr is going to raise your assert.

Upvotes: 1

Related Questions