0

I am not sure where I am going wrong with this.

I have a Movie.h with all the data members and constructors destructors and copy constructors needed but I have a feeling it's failing at my assignment operator someone, please help

  Movie& Movie::operator=(const Movie& _assign) {
    // Self-assignment check
    if (this == &_assign)
        return *this;

    // Shallow copy non-dynamic data members
    mRuntime = _assign.mRuntime;

    // Deep copy appropriate data members
    mTitle = new char[strlen(_assign.mTitle) + 1];
    strcpy_s(mTitle, strlen(_assign.mTitle) + 1, _assign.mTitle);

    // Deep copy the reviews
    SetStars(_assign.mStars, mNumReviews);

    return *this;
  }

  void Movie::SetStars(const int* _stars, int _numReviews) {
    
    // Allocate array and deep copy
    mStars = new int[_numReviews];

    for (int i = 0; i <= _numReviews; ++i) {
        // Cap reviews between 1-10
        if (_stars[i] > 10)
        {
            mStars[i] = 10;
        }
        else if (_stars[i] < 0)
        {
            mStars[i] = 0;
        }
        else
        {
            mStars[i] = _stars[i];
        }
    }

    // Set the number of reviews
    mNumReviews = _numReviews;
  }
bruno
  • 31,755
  • 7
  • 21
  • 36
Neteroh
  • 7
  • 3
  • 2
    Why are you using `char*` instead of `std::string`? – David C. Rankin Jul 07 '20 at 17:40
  • Unrelated: `mTitle` and `mStars` may have leaked pre-existing allocations. Obligatory link ot [What is the copy-and-swap idiom?](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) Copy and Swap's not always the best solution, but it's next to impossible to get it wrong and makes an excellent starting point. Profiling the code will let you know if you need something more efficient. – user4581301 Jul 07 '20 at 17:41
  • You want to tell us what line of code your program is crashing? You want to tell us what the exact actual error message is? It's literally easier to copy and paste the error than to paraphrase it with fewer words. – MPops Jul 07 '20 at 18:24
  • no, I was hoping for someone to tell me why am I getting the error which line is causing the trouble – Neteroh Jul 07 '20 at 18:34
  • @Neteroh, please post the whole error message. And if you insist on not using a debugger, place some `cout` statements on EVERY LINE in your program and tell us after which `cout` your program crashes. This is called "print debugging", and is generally frowned upon, because it requires an absurd amount of program modification. Or, you could use a debugger. – MPops Jul 07 '20 at 18:51
  • This is the error message im getting @MPops: Unhandled exception at 0x00007FFAB8ABA799 in Debugging.exe: Microsoft C++ exception: std::bad_array_new_length at memory location 0x0000008A3D9CEEB0. – Neteroh Jul 07 '20 at 19:12
  • it's been 3 hours. Have you learned how to use the debugger yet? Please tell us which line of code is causing this issue. Also, please update your original question with the code you said you have updated in other comments. If you don't do this, it's really hard to track what the problem with your code is. You said you changed something, so please update the question. – MPops Jul 07 '20 at 22:57

1 Answers1

3

The problem happens here:

mStars = new int[_numReviews];


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

Specifically here:

i <= _numReview // this causes you to go out of bounds

changing it to:

i < _numReview

resolves the issue

You are allocating _numReview items. C++ has 0-based array indexing. Elements will go from 0 to _numReview - 1

Please consider using std::string and std::vector instead of c-style arrays.

bhristov
  • 2,947
  • 2
  • 7
  • 25
  • @Neteroh it will be better if you use std::string instead of the char array. I suspect that this line is causing the issue: `strcpy_s(mTitle, strlen(_assign.mTitle) + 1, _assign.mTitle);` now. – bhristov Jul 07 '20 at 18:17
  • I can't it's for an assignment which I'm not allowed to change that trust me I much rather work with strings than arrays – Neteroh Jul 07 '20 at 18:33
  • 1
    @Neteroh Run the program in the debugger that comes with your development environment. A decent debugger will stop dead as soon as the program crashes and allow you to inspect the crash site and the back trace that lead up to the crash. This will give you the information you need to narrow down the cause if not solve it outright. – user4581301 Jul 07 '20 at 19:25