Run-Time Check Failure #2

Hi, Thanks everyone for the help so far and my apologies for the numerous post. This should be my last post if I'm able to get this problem resolved. I have implemented a function named to_string for the bitsetts class, but why does the program throw a "Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted"? after it has run the function? I am puzzled. Is it due to the size of the variable of bitsetts? But I am not allowed to change the size as I can't modify the main(). I don't quite understand what I need to do from here. Hope you are able to help. Thank you. Note: int main() cannot be modified since it is a school assignment.


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
#include <iostream>    
#include <vector>    

template<size_t k>
class bitsetts
{
private
	unsigned char array[number_of_char_you_need];
public:
	bitsetts()
	{
		memset(array, 0, sizeof(array));
	}

	unsigned char set(size_t pos, bool val = true)
	{
		return array[pos] = val;
	}
	const std::string to_string()
	{
		std::string str;
		str.push_back('0');
		str.push_back('0');
		for (unsigned int i = k; i-- > 2;)
		{
			str.push_back('0' + array[i - 2]);
		}
		return str;
	}
	bool test(size_t pos) const
	{
		if (((array[pos] & (1U)) != 0))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
int main()
{
	int index = 0;

	for (int index{}; index < 2; index++)
	{
		switch (index)
		{
			case 1:
			{
				try
				{
					bitsetts<5> bitsetts;
					bitsetts.set(1);
					bitsetts.set(2);

					const std::string s = bitsetts.to_string();
					if (str= "0010")
					{
						std::cout << s << std::endl;
						throw std::runtime_error{ "Converstion tothe string didnt work." };
					}
				}
				catch (const std::exception& exception)
				{
					std::cout
						<< "failed1 " << std::endl;

				}
			}
			break;
		}
	}
 
}
Last edited on
Is bitsetts::array supposed to be bit-packed or not? Because you're creating an array of bits/CHAR_BIT bytes, but you're accessing it as if each byte was representing an entire boolea value, without bit-packing.
You are not implementing it properly at all.
You've calculated how many bytes you need to store a given number of bits.
But then you only set/test entire bytes.

You need to use some bitwise operators to set/test the bits in the bytes.

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
#include <iostream>    
#include <vector>    
#include <climits>
#include <cstring>

template<size_t NumBits>
class bitsetts
{
private:
    static const unsigned NumBytes = (NumBits + CHAR_BIT - 1) / CHAR_BIT;
    unsigned char array[NumBytes];
public:
    bitsetts() { std::memset(array, 0, sizeof(array)); }

    void set(size_t bit) {
        array[bit / CHAR_BIT] |= (1U << bit % CHAR_BIT);
    }

    bool test(size_t bit) const {
        return array[bit / CHAR_BIT] & (1U << bit % CHAR_BIT);
    }

    const std::string to_string()
    {
        std::string str;
        for (unsigned i = NumBits; i-- > 0;) str.push_back('0' + test(i));
        return str;
    }


    friend std::ostream& operator<<(std::ostream& os, const bitsetts& b)
    {
        for (unsigned i = NumBits; i-- > 0; ) os << b.test(i);
        return os << '\n';
    }
};

int main()
{
    try
    {
        bitsetts<5> bitsetts;
        bitsetts.set(1);
        bitsetts.set(2);
        auto s = bitsetts.to_string();
        std::cout << s << std::endl;
        if (s != "00110")
            throw std::runtime_error{"Conversion to string did not work."};
    }
    catch (const std::exception& exception)
    {
        std::cout<< "failed1\n";
    }
}

Topic archived. No new replies allowed.