Fixing a segmentation fault

I believe that the segmentation fault is occurring because the object isn't getting written into the array correctly (I haven't worked much with arrays of objects before). But I'm not sure what to change to fix that. The actual error occurs when ageAnimals() is called.

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
#include "Zoo.hpp"
#include "Animal.hpp"
#include <iostream>
#include <string>

int main()
{
        Zoo zoo;
	
        zoo.animalInventory(); //initialize arrays

				
        int firstBuy;
	std::cout << "How many animals will you buy?" << std::endl;
        std::cin >> firstBuy;

        zoo.buyAnimal(new Animal(),firstBuy)
        
        //SEG FAULT OCCURS HERE
        zoo.ageAnimals();


	return 0;
}
             


Zoo.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef ZOO_HPP
#define ZOO_HPP
#include "Animal.hpp"

// Zoo class declaration

class Zoo
{
	private:

		Animal** generic;
		int genArraySize; 
		int numGeneric; //number of animal counter

	public:
		void buyAnimal(Animal* gen, int num);
		void animalInventory();
		void ageAnimals();

};


Zoo.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Zoo.hpp"
#include "Animal.hpp"
#include <iostream> //for cout and cin
#include <cstddef> //for using NULL

/*******************
 *buyAnimal
 *Looks to see if there is enough space in the correct animal array for another animal. Will call function to increase array size if not. Then adds new animal object to the array once it finds an empty space.
 * ****************/
void Zoo::buyAnimal(Animal* gen, int num)
{
	int counting=0;
	while (counting<num)
	{
		/*if (generic[genArraySize-1] != NULL) //if array is full
		{
			increaseInventory(); //double the array size
		}*/

		for (int count=0; count<genArraySize; count++)
		{
			if (generic[genArraySize]==NULL) //once empty space reached
			{
				std::cout << "Putting object in empty array element" << std::endl;
				generic[count]=gen; //add new object to array
				numGeneric++;
				count=genArraySize; //exit loop
			}
		}
	counting++;
		//call subcost in main 
	} 
}

/*******************
 *animalInventory
 *sets up the starting array sizes for animals, and sets everything to null
 * ****************/
void Zoo::animalInventory()
{
	generic = new Animal* [10]; //dynamic array of Animal objects
	genArraySize = 10;
	for (int count=0; count<genArraySize; count++)
	{
		generic[count]=NULL;
	}
}

/***************
 * ageAnimals
 * ages all animals in arrays by 1 day
 * *************/

void Zoo::ageAnimals()
{
	std::cout << "increase age" <<std::endl;

	for (int count=0; count<numGeneric; count++)
	{
		generic[count]->age1Day();	
	}
}


Animal.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ANIMAL_HPP
#define ANIMAL_HPP


// Animal class declaration

class Animal
{
	protected:
		int age;
	public:
		Animal(); //default constructor
		void age1Day();


};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Animal.hpp"

/**************
 *Animal()
 *default animal object
 * ***********/
Animal::Animal()
{
	age=1;
}

/****************
 * age1Day
 * will increase the age of the animal by 1 day
 * **************/

void Animal::age1Day()
{
	age++;		
}
Last edited on
Have you run the program with your debugger? The debugger should be able to tell you exactly where it detects the problem and you can view the contents of the variables at the time of the crash.

By the way why did you choose to use raw arrays instead of std::vector? And since you did go the raw array route where are your destructors, copy constructors, assignment operator, move constructor?

The assignment requires that I use dynamic arrays instead of vectors, unfortunately. Also, I'm still writing the program, so at the moment I'm just trying to get this one aspect of it to work before moving on to the next parts. Eventually it's supposed to have derived classes for different types of animals, random events that happen in the park, etc.
Okay, and what about the first question?

I just ran it through a debugger and it says it's coming from age1Day() somehow? from the age++


Program received signal SIGSEGV, Segmentation fault.
0x00000000004018da in Animal::age1Day (this=0x0) at Animal.cpp:88
88 age++;

That's were the problem is detected, now you need to find out why "this=0x0". So you need to backtrace until you find out why you're playing with a nullptr.

Well, I said at the start that I figured my object wasn't getting written into my array correctly... but I don't know what I'm doing wrong in regards to that.

I'm building the array and setting the elements to null
1
2
3
4
5
6
generic = new Animal* [10]; //dynamic array of Animal objects
genArraySize = 10;
for (int count=0; count<genArraySize; count++)
{
	generic[count]=NULL;
}


and then when a new animal is bought and added to this array I pass a new object to the function when it's called

1
2
3
4
int firstBuy;
std::cin >> firstBuy;

zoo.buyAnimal(new Animal(),firstBuy);


and this for loop looks for an empty array element and sets the element equal to the object passed to it ("gen")

1
2
3
4
5
6
7
8
9
10
for (int count=0; count<genArraySize; count++)
	{
		if (generic[genArraySize]==NULL) //once empty space reached
		{
			std::cout << "Putting object in empty array element" << std::endl;
			generic[count]=gen; //add new object to array
			numGeneric++;
			count=genArraySize; //exit loop
		}
	}


I don't know why it's coming out as zero
nevermind I see the new, its in a function call.

if (generic[genArraySize]==NULL) //once empty space reached
that looks like it really, really should have been [count]. What do you think?

I think this is hosing up buy animal.
Last edited on
@jonnin yes! Bless you sir or madam (haha). It was such a simple mistake... must be one of those things where you just look at something for too long and you gloss over it. Thank you so much :)
you are very welcome.
this kind of simple mistake are why pointers are avoided so much by experienced coders :) It compiles, it runs, and it blows up without a lot of help about why. Yuck!

Registered users can post here. Sign in or register to post.