Like Tree2Likes
  • 2 Post By JohnGraham

Access violation error

This is a discussion on Access violation error within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a getter function from my class, which would return a part of my member variable ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Access violation error

    I am trying to write a getter function from my class, which would return a part of my member variable vector<short>:
    Code:
    const vector<short> Input::getOneRow(int index) const
    {
    	try
    	{
    		return values.at(index);
    	}
    	catch(const out_of_range &e)
    	{
    		cerr << "Out of range " << endl;
    	}
    }
    And I try to test it with an out of range value for int index:
    Code:
    int main()
    {
    	/* ... declare myClass ... */
    
    	vector<short> test2 = myClass.getOneRow(555); 
    }
    What happens is that the exception is caught, the error message is printed, but then when my program crashes with some unknown error. Why? What happens?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    Sorry for double posting but I kind of figured out that the error was due to nothing being returned from the catch block, and in the main function nothing was to be assigned to the test2 vector. I have modified the functions slightly:
    Code:
    const vector<short>& Input::getOneRow(int index) const
    {
    	try
    	{
    		return values.at(index);
    	}
    	catch(const out_of_range &e)
    	{
    		cerr << "Out of range. Returning constant empty vector." << endl;
    		const static vector<short> bad;
    		return bad;
    	}
    }
    Now, due to efficiency reasons, I would like to return a constant reference to my vector and call it in this way:
    Code:
    int main()
    {
    	Input myClass("someArgument");
    
    	const vector<short> &temp = myClass.getOneRow(5000);
    
    }
    The 5000 index is deliberately made to be out of range. So the values.at will throw an out_of_range exception, the catch block will be activated, and what should I return from it? I do not really like the way it is now, returning a static vector, since it is not really sensible to occupy the memory with something that is never going to be used anyway (constant reference to an empty static vector...). Futhermore, the static vector does not go out of scope so it is there forever. Is there a better way to deal with this problem?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by kulfon View Post
    Is there a better way to deal with this problem?
    I'd let the exception be thrown:

    Code:
    const vector<short>& Input::getOneRow(int index) const
    {
        return values.at(index);
    }
    Your code is pretty much a text book case for throwing an exception - you (as the programmer of this specific function) don't know what to do to recover from the error, so you throw an exception and force someone else to take appropriate action. In this case, it looks like any out-of range index is a programming error, so you should definitely not try to mask it.
    Elysia and iMalc like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird access violation error with stack
    By ryeguy in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 01:53 AM
  2. Access Violation Error
    By Neurofiend in forum C Programming
    Replies: 3
    Last Post: 08-22-2005, 06:19 PM
  3. Access Violation error
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 04-30-2005, 11:33 AM
  4. Access Violation Error
    By Scott in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2003, 05:00 AM
  5. Help! Access violation error in C++
    By runlevel007 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2003, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21