This seems like a fairly strange bug to me. My C++ program is segfaulting, and I found something strange using GDB. I have the following constructor and copy constructor:
Bank::Bank(char mode, int floor_dimensions_, int num_floors_) : floor_dimensions(floor_dimensions_), num_floors(num_floors_) {
for (int i = 0; i < num_floors; i++) {
floors[i] = new Floor(floor_dimensions);
}
if (mode == 'M') {
read_map(floors);
} else if (mode == 'C') {
read_coords(floors);
}
}
Bank::Bank(const Bank& b) {
floor_dimensions = b.floor_dimensions;
num_floors = b.num_floors;
cout << floor_dimensions << endl;
cout << num_floors << endl;
for (int i = 0; i < num_floors; i++) {
floors[i] = new Floor(*b.floors[i]);
floors[i]->print_map();
}
The definition of this class is something like
class Bank {
/** The number of floors the bank has */
int num_floors;
/** The dimension of each of the floors */
int floor_dimensions;
/** The floors in the bank */
Floor* floors[];
private:
Bank(char mode, int floor_dimensions_, int num_floors_);
Bank(const Bank& bank);
~Bank();
void read_map(Floor** floor);
void read_coords(Floor** floor);
}
Now I set a breakpoint at the first line of the constructor using GDB. After executing the next two lines, the value of b.floors[1] has changed, which leads to the segfault when I call b.floors[i]->foo().
Breakpoint 1, Bank::Bank (this=0x7fffffffe050, b=...) at bank.cpp:29
29 floor_dimensions = b.floor_dimensions;
(gdb) p b.floors[0]
$4 = (Floor *) 0x610070
(gdb) p b.floors[1]
$5 = (Floor *) 0x6103f0
(gdb) p b.floors[2]
$6 = (Floor *) 0x610770
(gdb) n
30 num_floors = b.num_floors;
(gdb) p b.floors[0]
$7 = (Floor *) 0x610070
(gdb) p b.floors[1]
$8 = (Floor *) 0x8006103f0
(gdb) p b.floors[2]
$9 = (Floor *) 0x610770
(gdb) n
32 cout << floor_dimensions << endl;
(gdb) p b.floors[0]
$10 = (Floor *) 0x610070
(gdb) p b.floors[1]
$11 = (Floor *) 0x800000003
(gdb) p b.floors[2]
$12 = (Floor *) 0x610770
Does anyone have any idea what’s going on?
The problem with your code is most likely the fact that you are attempting to access an uninitialized pointer (
b.floors). Assumingbwas a result of this copy constructor, thenb.floorswill be pointing to random memory.b.floors[i]will then be pointing to random memory some bytes ahead of whatb.floorsis pointing to, and eventually you use->to dereference this random address, which causes a segmentation fault.The solution to your problem is to properly allocate and copy the floors array:
You may also want to copy the pointed object
Floor, depending on the details of your implementation.