Polymorphism Question

Hi all,

Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class base
{
   public:
      base();
};

class derived : public base
{
   public:
      derived();
};

class foo
{
   public:
      foo();
      ~foo();
      base* get_bar(const int i) const;

   private:
      vector<base*> bar;
};

foo::foo()
{
   bar.resize(2);
   bar[0] = new base;
   bar[1] = new derived;
}

foo::~foo()
{
   delete bar[0];
   delete bar[1];
}

base* foo::get_bar(const int i) const
{
   if(i > 0 && i < bar.size())
      return bar[i];
   else
      throw out_of_range;
}


When I call foo::get_bar(1);, I get a segmentation fault. Why is this happening? Since the vector is of pointers to the base class, making it point to a derived class should be fine right?
Your understanding is correct. foo::get_bar(1) should not cause a segfault.

Can you post a full program that reproduces the problem?
Well, I have 12 different source and header files with a few thousand lines of code, so that's probably not advisable haha. However, I have uploaded all of the files to Google Docs, I don't blame you if you don't want to look through this...

https://docs.google.com/open?id=0B9BBTkygM0-6MjFkNmE5MzAtZTRkZi00ZjcwLWFiOGYtYWM4Yzg4YzNjYmRi

Please pay special attention to line 12 of beast_manager.cpp, this is the function call that causes the segmentation fault.

EDIT:
I also tried catching all 9 kinds of errors from stdexcept, to no avail. The program isn't throwing any one of those, it's something else...

EDIT again:
I successfully caught exception and exception::what() returns
St9bad_alloc

Line 246 in map.cpp is where, I am pretty sure, that the bad_alloc is coming from.
Last edited on
Steps I took to debug:


1) Run the program, select option '5' from the menu to generate a random map

2) Program crashes, popup box asks if I want to debug, I say yes

3) VS breaks on the line of offending code, which is in some internal-to-VS source file (stdthrow.cpp).

4) Using the call stack viewer in VS, I backtrack up to the line that is actually calling a standard function.

5) Call stack takes me to here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void map::get_random_coord(int &x, int &y)
{
    int size = height*width;
    int num = 0;
    
    do{
        num = rand() % size; //Generate random number
        x = ((num%width)-1); //Convert to x
        y = (num/width); // Convert to y
    }while(x==0 || x==(width-1) || y==0 || y==(height-1));
    
    while(grid[y][x].game_piece != NULL){   // <<<<<<<<<<<<<<<<< THIS LINE
        if(grid[y][x].game_piece != NULL)
            get_random_coord(x, y);
    }
    
    return;
}


6) I put 'y' and 'x' in the watch window so I can see their values. y=17 and x=-1

7) I immediately recognize that -1 is an invalid index for a vector.

8) I look at how x is calculated, it seems you are subtracting 1 from it (why?)

9) I remove the -1, rebuild, rerun. It no longer crashes (at least not when I pick that option)


The only other way I could get it to crash (that I tried) was to type "exit" at the main menu, rather than selecting one of the available options. How did you get it to crash in beast_manager.cpp?
Topic archived. No new replies allowed.