Hey guys,

I am still pretty new to C++ and am completely self-taught up to this point. I am currently learning about try, throw, and catch statements for exceptions and am trying to incorporate this into my programs. I know that normally when you allocate new dynamic memory, if the allocation fails you will receive a bad_alloc exception. Below is the portion of my code where this allocation takes place:

struct applicant{
		char degree;
		int age;
		int exp;
		bool interview;
		int finalScore;
	};

	int num;
	cout << "Please enter the number of applicants (1 - 10):";
	cin >> num;

	try
	{
		applicant *applicants = new applicant[num];
	}
	catch(bad_alloc&)
	{
		cout << "Error: Dynamic Memory Allocation Failed.  Program Terminating."
			 << endl;
		cin.get();
		return 0;
	}

If the program encounters an exception on the memory allocation, I want the program to display the above line and then exit. Please note that in forming this code, I looked at the following website for proper syntax: http://cplusplus.com/doc/tutorial/exceptions/ (towards the bottom it explains exceptions for dynamic memory allocation)

Now, before I put this allocation into a try block and added the catch block, the program compiled and ran as expected (I am using Visual C++ 2008 Express with windows XP SP3). Now the program will not compile at all. All of the errors that I am receiving point to any place that I make any reference to a member of my applicant struct (i.e. displaying something, getting user input, etc...). Below are some of the errors I am receiving:

1>c:\documents and settings\drake reporto\my documents\visual studio 2008\projects\dynamicinterview\dynamicinterview\sourcecode.cpp(41) : error C2065: 'applicants' : undeclared identifier
1>c:\documents and settings\drake reporto\my documents\visual studio 2008\projects\dynamicinterview\dynamicinterview\sourcecode.cpp(41) : error C2228: left of '.degree' must have class/struct/union
1>c:\documents and settings\drake reporto\my documents\visual studio 2008\projects\dynamicinterview\dynamicinterview\sourcecode.cpp(43) : error C2065: 'applicants' : undeclared identifier
1>c:\documents and settings\drake reporto\my documents\visual studio 2008\projects\dynamicinterview\dynamicinterview\sourcecode.cpp(43) : error C2228: left of '.age' must have class/struct/union

Here are lines 41 - 44 for your reference:

cout << "Age: ";
cin >> applicants[i].age;
cout << "Years Experience: ";
cin >> applicants[i].exp;

Can anybody help point me in the correct direction of how to fix this?

-D

Get ready to slap yourself in the face dgr.

You're defining the applicants variable as a local variable (a variable is only valid between the brackets ( { } ) where it is defined in.

try
{
applicant *applicants = new applicant[num];
}

After the } the applicants isn't valid anymore.

Also note that you are using 'new', so you are responsible for cleaning up the memory when applicants isn't needed anymore with 'delete[] applicants;' (it's an array so you need delete[] and not delete).

Hope that helped.

Get ready to slap yourself in the face dgr.

You're defining the applicants variable as a local variable (a variable is only valid between the brackets ( { } ) where it is defined in.

After the } the applicants isn't valid anymore.

Also note that you are using 'new', so you are responsible for cleaning up the memory when applicants isn't needed anymore with 'delete[] applicants;' (it's an array so you need delete[] and not delete).

Hope that helped.

Thank you for the response. As to your second point, I do delete the allocated memory towards the end of the program but I didn't post that portion of the code as I didn't think it was applicable to my question.

As to your first point, I now understand why that is happening, but if I still want to attempt to catch this exception, how should I go about doing so? Should I basically put my entire code into the try block (essentially my entire code uses the dynamically allocated memory)?

Nevermind, I have this working now. Thank you for your help and I will now mark this thread as solved.

-D

Did you fix it the way you stated? If so ... it is not necessary to put the entire code in a try-catch block.

I did not place the entire code in the try block. I placed the code from the point of where I allocate the dynamic memory through all of the references to members of the struct and to the point where I delete it.

-D

Ok, only the allocation should be enough - after this you know that it is properly allocated so it won't throw a bad_alloc anymore.

So you could do:

applicant *applicants;
try {
applicants = new applicant[num];
} catch ( whatever )
{
}
reference the applicants here

There really is no advantage to it, but I like to keep my try catch blocks small.

This question has already been answered. Start a new discussion instead.