Memory usage problem

the memory usage of the below program constantly increases. how to fix this problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>

class ChoiceTest
{
public:
    ChoiceTest(int questionsCount)
    {
        this->questionsCount = questionsCount;
        answers = new int[questionsCount];
        for (int i = 0; i < questionsCount; i++)
        {
            answers[i] = -1;
        }
    }

    void setAnswer(int questionIndex, int answer)
    {
        answers[questionIndex] = answer;
    }

    int getAnswer(int questionIndex) const
    {
        return answers[questionIndex];
    }

protected:
    int questionsCount;

private:
    int* answers;
};

class TimedChoiceTest : public ChoiceTest
{
public:
    TimedChoiceTest(int questionsCount)
        : ChoiceTest(questionsCount)
    {
        times = new int[questionsCount];
        for (int i = 0; i < questionsCount; i++)
        {
            times[i] = 0;
        }
    }

    void setTime(int questionIndex, int time)
    {
        times[questionIndex] = time;
    }

    int getTime(int questionIndex) const
    {
        return times[questionIndex];
    }

private:
    int* times;
};

#ifndef RunTests
void executeTest()
{
    ChoiceTest test(5);
    for (int i = 0; i < 5; i++)
    {
        test.setAnswer(i, i);
    }

    for (int i = 0; i < 5; i++)
    {
        std::cout << "Question " << i + 1 << ", correct answer: " << test.getAnswer(i) << "\n";
    }
}

int main()
{
    for (int i = 0; i < 3; i++)
    {
        std::cout << "Test: " << i + 1 << "\n";
        executeTest();
    }
}
#endif
Last edited on
The classes need destructors that free the allocated memory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <algorithm>

class ChoiceTest {
public:
	ChoiceTest(int questionsCount_) : questionsCount(questionsCount_), answers(new int[questionsCount_]) {
		std::fill_n(answers, questionsCount, -1);
	}

	~ChoiceTest() { delete[] answers; }

	ChoiceTest(const ChoiceTest&) = delete;
	ChoiceTest& operator=(const ChoiceTest&) = delete;

	void setAnswer(size_t questionIndex, int answer) { answers[questionIndex] = answer; }
	int getAnswer(size_t questionIndex) const { return answers[questionIndex]; }

protected:
	size_t questionsCount {};

private:
	int* answers {};
};

class TimedChoiceTest : public ChoiceTest {
public:
	TimedChoiceTest(size_t questionsCount) : ChoiceTest(questionsCount), times(new int[questionsCount] {}) {}

	~TimedChoiceTest() { delete[] times; }

	TimedChoiceTest(const TimedChoiceTest&) = delete;
	TimedChoiceTest operator=(const TimedChoiceTest&) = delete;

	void setTime(size_t questionIndex, int time) { times[questionIndex] = time; }
	int getTime(size_t questionIndex) const { return times[questionIndex]; }

private:
	int* times {};
};

void executeTest()
{
	ChoiceTest test(5);

	for (size_t i = 0; i < 5; ++i)
		test.setAnswer(i, i);

	for (size_t i = 0; i < 5; ++i)
		std::cout << "Question " << i + 1 << ", correct answer: " << test.getAnswer(i) << "\n";
}

int main()
{
	for (size_t i = 0; i < 3; ++i) {
		std::cout << "Test: " << i + 1 << "\n";
		executeTest();
	}
}

Topic archived. No new replies allowed.