Exercise 7.1

The following function provides absolutely no checking of either possible bad data or the possible failure of an operation. Identify all the things that might possibly go wrong within the function (in this exercise, we don’t yet worry about possible exceptions raised).

					int *alloc_and_init( string file_name )
					{
					ifstream infile( file_name );
					int elem_cnt;
					infile >> elem_cnt;
					int *pi = allocate_array( elem_cnt );
					int elem;
					int index = 0;
					while ( infile >> elem )
					pi[ index++ ] = elem;
					sort_array( pi, elem_cnt );
					register_data( pi );
					return pi;
					}
				

The first error is a type violation: The ifstream constructor requires a const char* and not a string. To retrieve the C-style character string representation, we invoke the c_str() string ...

Get Essential C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.