std::vector access violation

This is a discussion on std::vector access violation within the C++ Programming forums, part of the General Programming Boards category; I'm new to the std vector class, and recently implimented a class structure similar to the one in the program ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    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.

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

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

    look at this: http://www.sgi.com/tech/stl/Vector.html

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

    Smile

    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
    Location
    Sweden
    Posts
    3,145
    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?
    MagosX.com

    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
    Posts
    2,544
    I think you want the back() method which returns a reference to the last element:
    Code:
    	s& lastItem = db.back();
    	lastItem.v.push_back(4);

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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();
    --p;
    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, 11:52 AM
  2. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 11: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, 10:06 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

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