Thread: std::vector access violation

  1. #1
    Registered User
    Join Date
    Sep 2004

    Unhappy std::vector access violation

    I'm new to the std vector class, and recently implimented a class structure similar to the one in the program below. When I received an "Access Violation" error when trying to push something into the vector, I recreated the problem in a simpler program (show below). I'm assuming that the vector has no room to grow within a structure and causing an error message, but am not sure how the vector actually works. If that is the case, I would greatly appreciate a suggestion on how to use an alternative method of achieving a similar system using the vector class. I'm hoping that I'm just using it wrong though.

    #include <vector>
    using namespace std;
    struct s{
    	vector<int> v;
    int main(){
    	vector<s> db;
    	s ns;
    	s *p = db.end();
    	return 0;
    please help me!

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    db.end() returns an iterator, not a vector element.

    look at this:

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Manassas, VA USA


    I'm glad i'm seeing this thread 'cause i'm about to start an intensive study on the vector class.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    std::vector.end() returns an iterator to the element AFTER the last element. Use db[db.size() - 1].v.push_back(4) or something instead?

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    I think you want the back() method which returns a reference to the last element:
    	s& lastItem = db.back();

  6. #6
    Registered User
    Join Date
    Mar 2002
    make p vector<s>::iterator and assign it the value of db.end(). Decrement the value of p by 1. Use the arrow operator on p to get the last element of v, which can be obtained using the back() method of vectors.

    vector<s>::iterator p = db.end();
    cout << p->v.back();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  3. SigSegv Violation
    By Mellowz in forum C Programming
    Replies: 3
    Last Post: 02-23-2005, 03:06 AM
  4. Weird Acception Violation :: MFC
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 07-15-2002, 09:06 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM