0

I am trying to return a string pointer from my method GetOutliers() to pass my test case but it is not working and I keep getting the error "FAILED: {Unknown expression after the reported line} due to unexpected exception with message: std::bad_alloc . I have already tried to define the method without the pointer but that doesn't work because in the test case it does string* outlier = bama.GetOutliers() so it is expecting the output to be a string pointer. I have also tried it with the pointer and I made it return an array with my string stored in it. Both attempts gave me the same error.

I need help trying to figure out how to return the string totalString and hopefully pass my test case.

Attempt 1:

string* State::GetOutliers(){
    vector<string>outliers;
    double mean = GetMeanDeaths();
    double std = Stdev();
    int deathNum = week->GetDeathCount();
    string getOut = " - total deaths: ";
    string death = to_string(deathNum);
    string week = week;
    string* totalString;
    string sentence;
    int outCount;

    for(int i = 0; i<numberOfDataPoints;i++){
        if(deathNum >= (2*std+mean)){
            sentence = week+getOut+death;
        }
        outCount++;
    }
    totalString = &sentence;
    return totalString;
}

Attempt 2:

string* State::GetOutliers(){
    vector<string>outliers;
    double mean = GetMeanDeaths();
    double std = Stdev();
    int deathNum = week->GetDeathCount();
    string getOut = " - total deaths: ";
    string death = to_string(deathNum);
    string week = week;
    string totalString;
    string stringArray[numberOfDataPoints];

    string sentence;
    int outCount;

    for(int i = 0; i<numberOfDataPoints;i++){
        if(deathNum >= (2*std+mean)){
            totalString = week+getOut+death;
            stringArray[i] = totalString;
        }
        outCount++;
    }
    return stringArray;
}

Test case:

TEST_CASE("Testing State object")
{
    State bama("Alabama");
    CHECK("Alabama" == bama.GetName());

    bama.AddWeek("2020-05-02,2000");
    bama.AddWeek("2020-05-09,2000");
    bama.AddWeek("2020-05-16,100");
    bama.AddWeek("2020-05-23,2000");
    bama.AddWeek("2020-05-30,2000");
    bama.AddWeek("2020-06-06,2000");

    CHECK(Approx(1683.33).epsilon(0.01) == bama.GetMeanDeaths());

    CHECK(Approx(708.08).epsilon(0.01) == bama.Stdev());

    CHECK(1 == bama.GetOutlierCount());
    std::string* outliers = bama.GetOutliers();
    CHECK("2020-05-16 - total deaths: 100" == outliers[0]); //TEST CASE FOR GetOutliers()
    delete [] outliers;
}
Beri Gill
  • 1
  • 1
  • Welcome to Stack Overflow. Please look over our [help section](https://stackoverflow.com/help), with special attention to the page on [minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example). This code is not complete (i.e. we can't reproduce the error without filling in a lot of missing code), and probably not minimal (i.e. you could probably remove some of this code and still get the same error). – Beta Oct 14 '22 at 02:41
  • 1
    Does this answer your question? [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). If you must return a pointer, it needs to be a pointer to something that won't be destroyed immediately. – Raymond Chen Oct 14 '22 at 02:48

0 Answers0