1

I'm having a bit of trouble figuring out exactly what I am doing wrong here, and haven't found any posts with the same issue. I am using a dynamic array of strings to hold a binary tree with the root at [0], first row of children, left to right, at [1] and [2], etc. While I haven't debugged that output format yet, I am much more concerned as to why that specific line is crashing my program.

I thought it was a pointer de-referencing issue, but outStream << &contestList[i] prints addresses as I'd expect, and outStream << *contestList[i] throws errors as I'd expect them to.

//3 lines are from other functions/files
typedef string elementType;
typedef elementType* elementTypePtr;
elementTypePtr contestList = new elementType[arraySize];

void BinTreeTourneyArray::printDownward(ostream &outStream)
{
    int row = 1;

    for (int i = 0; i < getArraySize(); i++)
    {

        outStream << contestList[i]; //this is crashing the program

        if (isPowerOfTwo(i))
        {
            outStream << endl;
            row++;
        }
        else
        {
            outStream << ":";
        }
    }
}

arraySize is a private member arraySize = ((2 * contestants) - 1) where contestants is the number of contestants in my tournament. Each round or "row" in the tree is synonymous with a tournament bracket. If there are n contestants, then there are 2n-1 nodes needed in the tree. The issue wouldn't be with this function.

getArraySize() { return arraySize; }
5
  • Show code for arraySize and getArraySize Commented Dec 3, 2017 at 21:14
  • "A string pointer array" Shudder! Any concise reasons to use arrays or pointers with your code? Could you elaborate about these please? Commented Dec 3, 2017 at 21:15
  • it's added @pepperjack Commented Dec 3, 2017 at 21:47
  • What is the value of arraySize when you call new? arraySize modified in any other place? Commented Dec 3, 2017 at 22:06
  • You probably shouldn’t use a raw pointer for your contestList. Instead, consider a std::vector<elementType>, or if the space overhead is a concern, std::unique_ptr<elementType[]>. Commented Dec 21, 2017 at 18:27

1 Answer 1

1

Turns out elementTypePtr contestList = new elementType[arraySize]; was the issue. contestList was a private member of the class, then I threw this line in a function declaring a local variable of the same name that disappears after the function ends. No biggie, except for the fact that I needed it in the print function...Oops.

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

1 Comment

And this illustrates two of the reasons why I suggested unique_ptr in the question comments above. 1. This means you were leaking wenever you called the function which created contestList; a unique_ptr would have avoided that. 2. More importantly, with unique_ptr, this would have left the class member at nullptr, which would have made this a lot easier to debug.

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.