STL vector size issues after declaration and before initialization

A seemingly simple thing that is driving me crazy. If I declare 3 vectors in the ofApp header file and then start adding to them (push_back) I get an EXC_BAD_ACCESS (code=2, address=0x20000) error with the ofVector overloaded = operator. When I query that size of the vectors before I do anything with them, the first one is always zero but the others can look enormous.

I am using Xcode 6.1.1 and oF8.4 and output would be:

size of particles1: 0
size of particles2: 4294961909
size of particles3: 110

and get EXC_BAD_ACCESS (code=2, address=0x20000) failure

Interestingly, if I run it on code blocks/windows/oF8.0 I get:

   size of particles1: 0
    size of particles2: 0
    size of particles3: 0

and it runs great …

I am using the super simple oF vector example and I just added two additional vectors from what was in the example …

Trying to use vectors on a different project but having the same problem. Most examples of best practices with vectors declare the vector then start with a push_back to populate. I typically try to initialize all variables before using them but in this case I don’t know the size the vector will be before I start and was hoping to just follow the typical declare then start push-back paradigm to grow it. Can anyone tell me what I am doing wrong? Thanks…

ofApp.h

#pragma once

#include "ofMain.h"

// create the particle class in the header for brevity

class Particle{
public:
    Particle(){
        pos.set( 0, 0, 0 );
        vel.set( ofRandom(-5,5), ofRandom(-4,-8), 0);
    }
    
    void update(){
        pos += vel;
        vel.y += .1;
    }
    
    void draw(){
        ofEllipse(pos.x,pos.y,10,10);
    }
    
    ofPoint pos;
    ofVec3f vel;
    
};

class ofApp : public ofBaseApp{
public:
    void setup();
    void update();
    void draw();
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    
    // declare the vector of particles
    vector<Particle> particles;
    vector<Particle> particles2;
    vector<Particle> particles3;
};

oFApp.cpp

#include "ofApp.h"

// write our boolean remove function
bool shouldRemove(Particle &p){
    if(p.pos.y > ofGetHeight() )return true;
    else return false;
}


//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(60);
    cout  << " size of particle1: " << particles.size()  << endl;
    cout  << " size of particle2: " << particles2.size()  << endl;
    cout  << " size of particle3: " << particles3.size()  << endl;
    
//     particles.clear();
//    particles2.clear();
//    particles3.clear();
    
//        particles.resize(1);
//       particles2.resize(1);
////    particles3.resize(1);
    
 //   particles.reserve(5);
  //  particles2.reserve(5);
 //   particles3.reserve(5);
    
//    cout  << " POST CLEAR size of particle1: " << particles.size()  << endl;
 //   cout  << " POST CLEAR size of particle2: " << particles2.size()  << endl;
 //   cout  << " POST CLEAR size of particle3: " << particles3.size()  << endl;
 
}

//--------------------------------------------------------------
void ofApp::update(){
    
    for(vector<Particle>::iterator it = particles.begin(); it != particles.end(); it++)
    {
        (*it).update();
    }
    ofRemove(particles,shouldRemove);
    
    for(vector<Particle>::iterator it = particles2.begin(); it != particles2.end(); it++)
    {
        (*it).update();
    }
    ofRemove(particles2,shouldRemove);
    
    
    for(vector<Particle>::iterator it = particles3.begin(); it != particles3.end(); it++)
    {
        (*it).update();
    }
    ofRemove(particles3,shouldRemove);

    
    
    cout  << " RUNNING size of particle1: " << particles.size()  << endl;
    cout  << " RUNNING size of particle2: " << particles2.size()  << endl;
    cout  << " RUNNING size of particle3: " << particles3.size()  << endl;
    
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
    for(vector<Particle>::iterator it = particles.begin(); it != particles.end(); it++)
    {
        (*it).draw();
    }
    
    
    ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
    for(vector<Particle>::iterator it = particles2.begin(); it != particles2.end(); it++)
    {
        (*it).draw();
    }
    
    
    ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
    for(vector<Particle>::iterator it = particles3.begin(); it != particles3.end(); it++)
    {
        (*it).draw();
    }

    
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    
   particles.push_back( Particle() );
    particles2.push_back( Particle() );
    particles3.push_back( Particle() );
    
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

Originally from
http://www.openframeworks.cc/tutorials/c++%20concepts/001_stl_vectors_basic.html

Giving your code a look-over, it does look like you should be fine. The behaviour you’re seeing would imply that there’s something going on with uninitialized memory, or maybe really weird iterator behaviour (I was looking to see if you were doing something like starting an iterator with particles2.begin() and terminating at particles3.end() but it looks like you haven’t done anything like that).

For what it’s worth, I ran your code and it seemed to work just fine. Is it possible there’s some code missing in your post, or that you’ve tweaked it a bit between testing and posting here?

Thanks admsyn, The code is verbatim.

To verify, it sounds like I am OK with declaring then pushing back vector without first having to clear/resize/reserve it. Might be slower than allocating first but I shouldn’t break anything?

I also tested it successfully on a different machine - xcode5.1.1/oF8.0/10.9SDK/32bit/LLVM5.1

I was finally able to get it to run on the orig machine but I am not sure why/how. I was changing the deployment target from 10.6(default) to 10.9 on app and oFframework and it worked (all zeros after declaration) and it even worked when I set it back to 10.6 so not sure this was it. Would the target specified have something to do with it? Too far back and it doesn’t have all the STL features? Now I am worried I don’t know root cause and it might cause me hidden problems again.

So it now runs successfully with osx10.9.5/oF8.4/xcode6.1.1/10.10sdk(app&oFapp)/10.9deploymentoFApp/10.6deploymentApp

Also wondering if there is a better way in Xcode to see the contents of a vector? The “variables view” debug window doesn’t show all the detail and limits the number of items from what I was able to figure out. Thanks