1

My homework is that I have to make a class (register) which contains 3 class arrays (birds, mammals, reptiles) which are in the animal class. Animal is the friend of Register. I will only show the birds part, to keep it simple.

The register class looks like:

class Register
{
    Bird* birds;
    unsigned int birdSize;
public:
    ...
}

The constructor of register:

Register::Register()
{
    this->birds = new Bird[0];
    this->birdSize = NULL;    
}

Now I have a function in register that adds one element to the birds array, the input is cin.

void Register::add()
{
 ...
        if (birdSize == 0)
        {
            birds = new Bird[0];
            Bird* temp = new Bird[0];
            temp[0].add();
            this->birds = temp;
            birdSize++;
        }
        else
        {
            Bird* temp = new Bird[birdSize+1];
            for (unsigned int i=0; i<=birdSize; i++)
            {
                temp[i] = this->birds[i];   
            }
            temp[birdSize+1].add();
            birds = new Bird[birdSize+1];
            birds = temp;
            birdSize++;
        }

temp[0].add() has the cin, it works properly. When I run the program, the user has to add 2 birds to the array. The problem occurs when reaching the part under 'else', so the second element of the array. The program surely reaches "temp[birdSize+1].add();" while running, then the "xyz.exe has stopped working" window pops up and it says in the details " Fault Module Name: StackHash_7e8e" so I'm sure something is wrong with the memory allocation, but the problem is that when I try to find the problematic line in debug mode, everything works fine.

Well, not everything. The program has a print() function, it prints out everything in Register. The second element of the array is the same as the first.

I have no clue what to do. I read many forum posts, read a cpp book, watched online tutorials, but I can't find the solution for this problem. Please help.

1
  • 1
    Dynamic arrays in c++ are std::vector<> instances! Commented May 11, 2014 at 14:39

1 Answer 1

2

Array index starts from 0. So in else part you are writing

 Bird* temp = new Bird[birdSize+1]; // size =birdSize +1;

So valid index range will be 0 -> birdSize, not birdSize+1.

The problem is

temp[birdSize+1].add();

you are using birdSize+1th index. It should be

temp[birdSize].add();

There are other bugs in your code:

for (unsigned int i=0; i<=birdSize; i++) // should be i<birdSize
{
  temp[i] = this->birds[i];   
}

There are other bad coding in your program:

Register::Register()
{
  this->birds = new Bird[0]; // should be this->birds=NULL
  this->birdSize = NULL;    // should be this->birdSize = 0
}

And obviously if your homework does not demand it, you should not use arrays in this way. For variable size container, use vector, list.... Array is only when the size is fixed.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. I'll going to reprogram it using vectors, then I'll report in how it went. Thank you!
Of course, I wanted to do it but it has a 5 minute time limit or so. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.