Thread: passing vector<Base*> reference gives segmentation fault

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    75

    Lightbulb passing vector<Base*> reference gives segmentation fault

    Code:
    int main() {
    vector< Base*> scene_;
    
    get_input(scene_);
    
    for(size_t i =0; i < scene_.size(); i++)
    {
    
    std::cout << scene_[i]->getSomething();
    
    }
    
    return1;
    
    }
    
    
    void get_input( vector<Base*>&scene) {
    
    
    Derived obj_a(some_param);
    
    Derived obj_b(some_other_param);
    
    
    
    scene.push_back(dynamic_cast<Base*> (&obj_a));
    
    scene.push_back(dynamic_cast<Base*> (&obj_b));
    
    }

    I also have
    Code:
    class Derived : public Base {};
    So I am trying to populate scene_ which is a vector of Base* objects which actually contains derived class objects;

    I am encountering a segmentation fault in this code while I am iterating through the scene_

    Last edited by progmateur; 02-08-2019 at 08:30 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,036
    Code:
    Derived obj_a(some_param);
    Derived obj_b(some_other_param);
    scene.push_back(dynamic_cast<Base*> (&obj_a));
    scene.push_back(dynamic_cast<Base*> (&obj_b));
    Your objects go out of scope when the function returns.

    Try this.
    Code:
    Derived *obj_a = new Derived(some_param);
    Derived *obj_b = new Derived(some_other_param);
    
    scene.push_back(dynamic_cast<Base*> (obj_a));
    scene.push_back(dynamic_cast<Base*> (obj_b));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    75
    Quote Originally Posted by Salem View Post
    Code:
    Derived obj_a(some_param);
    Derived obj_b(some_other_param);
    scene.push_back(dynamic_cast<Base*> (&obj_a));
    scene.push_back(dynamic_cast<Base*> (&obj_b));
    Your objects go out of scope when the function returns.

    Try this.
    Code:
    Derived *obj_a = new Derived(some_param);
    Derived *obj_b = new Derived(some_other_param);
    
    scene.push_back(dynamic_cast<Base*> (obj_a));
    scene.push_back(dynamic_cast<Base*> (obj_b));
    Thank you for your time and replying.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,529
    By the way, you shouldn't use dynamic_cast here. The pointer to Derived would be implicitly converted to a pointer to Base.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-24-2016, 05:02 AM
  2. Error while passing a reference of a vector to a function.
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2011, 06:36 AM
  3. Passing Vector as a Reference
    By gbman88 in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2011, 02:30 AM
  4. pointer passing segmentation fault
    By kapil1089thekin in forum C Programming
    Replies: 10
    Last Post: 01-04-2011, 02:03 PM
  5. Segmentation fault: vector to function
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2008, 04:36 AM

Tags for this Thread