简体   繁体   中英

Overwriting Objects in C++

I'm working on an assignment for school that implements a specific algorithm using a binary tree.

I've worked out the algorithm and my main() runs correctly on the first iteration, but seg faults right after that (You're supposed to be able to continuously run the program and simulate the algorithm without having to run ./main again, hence the while loop).

I have a feeling that it has to do with my BinaryTree *tree being created and used on the first round, and then being used again without being freed, but my attempts to work around this have been fruitless.

Here's my main that is giving the problem:

#include <iostream>
using namespace std;
#include "BinaryTree.h"
/* Prompts user to pick an option: 
 * first fit, best fit, or quit 
*/



int main (){

    bool quit;
    int command, elements, binSize, x, totalNodes;
    cout<<"Welcome to assignment 6!"<<endl;


    BinaryTree *tree = new BinaryTree();
    //Declare new tree for use in first fit algorithm

    while (!quit)
    {
      cout<<"Choose an option for the test: 1-> First fit, 2-> Best Fit, 3-> Quit"<<endl;
      cin>>command;

        /**************
            First Fit
        **************/

        if(command==1)
        {       
            cout<<"First Fit!\n";
            cout<<"Enter number of objects: ";
            cin>>  elements;
            cout<<"Enter capacities of bins: ";
            cin>>  binSize;
            cout<<"\n";



            x=1; 
            while (elements > x)    //Get next highest power of 2 
                x=x*2;      //to fill out bottom of binary tree

            totalNodes = 2*x -1;
            for(int i=1; i<=totalNodes; i++)
            {
                //cout<<"Adding node: "<<i<<endl;
                tree->AddItem(i, -1);
            }

            bool done = false;
            //elements = x;
            int a[elements];
            for (int i=1; i<=elements; i++)
            {
                cout<<"Enter space requirement of object "<<i<<endl;
                cin>>a[i];
            }

            for (int i=1; i<=elements && !done; i++)
            {
                tree->Insert(a[i], binSize, elements);
            }

               //Loop done, seg faults happens when called again
        }

The BinaryTree.cpp file is a bit long so I'll link them here if it's needed: http://pastebin.com/EtwdBp8N

Any advice or information on bad practice is much appreciated.

  int a[elements]; for (int i=1; i<=elements; i++) { cout<<"Enter space requirement of object "<<i<<endl; cin>>a[i]; } 

This bit is wrong, arrays are zero-based in C/C++, the loop should be:

  for (int i=0; i<elements; i++) 

If the debugger doesn't help, try running your program with valgrind http://valgrind.org/ or something similar. It will identify many invalid memory accesses for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM