   Function return type vector causing segmentation fault
  Hello again.  I seem to have run into some trouble when I try to return a vector from a function.  The program crashes and in debug mode it gives me an error stating "Program received signal SIGSEGV, segmentation fault."The program is crashing at a function returning a std::vector.  (More on this latter)First the class that defines the function in question.class bePhysicsEntity{    private:   char *name;   NewtonBody *thisbody;   vector<bePhysicsForce> forces;    public:   ...   vector<bePhysicsForce> GetForces();};Here is the definition of GetForces() as simple as can bevector<bePhysicsForce> bePhysicsEntity::GetForces(){    return forces;}So I am just wondering if there are any special rules about returning vectors and if I am missing something to cause this crash.Here is the portion of code where the crash actually takes place...void PhysicsApplyForceAndTorque(const NewtonBody* body){bePhysicsEntity *entity = reinterpret_cast<bePhysicsEntity*>(NewtonBodyGetUserData(body));    ...    if(entity)    {   for(unsigned int i=0; i < entity->GetForces().size(); i++)   {  ...   }    }}It crashes right when it attempts to evaluate the for loop.Sorry for the code dump but I want to provide any relevant information.Thanks in advance!   
  Nothing special is required to return a vector by value. Though, in this case you probably want to return by (const) reference instead of by value.   ;  you're returning a copy of that vector, which means copying the entire vector and returning that copy.  Depending on the size of the vector that could be super bad news performance-wise.  return const reference instead:const vector<bePhysicsForce> &GetForces() const{    return forces;}const means you can't add to it and stuff.  If you want to be able to do that then remove const from the declaration above but keep the ampersand.If it's seg-faulting it's probably because entity is a NULL or invalid pointer.  Inspect the value in your debugger and see if it makes sense.  I'm guessing you're not using a debugger tho... [smile]-me   ;  No the pointer to entity appears to be valid as I check it in the if statement which is backed up by the debugger indicating that entity is not NULL.  I will give the suggestion of using const and see what happens.   ;  Not NULL doesn't mean valid. A non-NULL pointer can be invalid if you passed the address of a temporary, a deleted object or a similar problem.   