*** glibc detected *** double free or corruption (out): 0x09fbd2d0 ***

Recently I have come across this error, I have found what is causing the error but don;t know how I fix it. From what I can tell the variable value is either corrupt or I have seen it mention that what I am trying to assign to the value may be too large. :s I don't believe std::vector has a size limit though.

This occurs after the creation of the third big integer

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

class bigint {
private:
	std::vector<unsigned char> value;
	libbase::Arith ar;
public:
	/*! \name Law of the Big Three */
	//! Destructor
	virtual ~bigint() {
		// Destroy vector contain big integer number
		if(!value.empty())value.clear(); // Not Offending
	}

	// May not be neccessary
	/*! \name Constructors / Destructors */
	//! Default constructor
	explicit bigint(signed long int x = 0) {
		value.push_back('0');
	}
	// @}

	friend std::istream& operator>>(std::istream& sin, bigint& x) {

		libbase::BaseConverter bc;

		unsigned char* uc = new unsigned char();
		sin >> uc;

		for (int i = 0; i < strlen((char*) uc); i++) {
			printf("%c", uc[i]);
		}

		std::cout << std::endl;

		std::vector<unsigned char> hexrep;

		for (int i = 0; i < strlen((char*) uc); i++)
			hexrep.push_back(uc[i]);

		std::vector<unsigned char> binrep;

		std::cout << "create bin rep" << std::endl;

		binrep = bc.hextobin(hexrep);


		std::cout << "create bin rep given data" << std::endl;

WHERE ERROR IS THROWN assignment fails - x.value = bc.bintodec(binrep);



		std::cout << "X given a value" << std::endl;

		for (int i = 0; i < x.value.size(); i++) {
			printf("%c", x.value.at(i));
		}
		std::cout << std::endl;

		return sin;
	}

};



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
	//! Convert a hexadecimal value to its binary representation
	std::vector<unsigned char> hextobin(const std::vector<unsigned char> &hexrep) {
		std::vector<unsigned char> binrep;

		std::string temp = "";

		for (int i = 0; i < (int) hexrep.size(); i++) {
			temp += digittobin(hexrep.at(i));
		}

		for (int i = 0; i < (int) temp.size(); i++) {
			binrep.push_back(temp[i]);
		}

		return binrep;
	}

std::vector<unsigned char> bintodec(const std::vector<unsigned char> &binrep) {
		// take each binary digits
		// times digit by its binary value e.g. 128*1 or 64*0
		// add to sum
		// Move to next binary value (binary value * 2)

		std::vector<unsigned char> value;
		value.push_back('1');

		std::vector<unsigned char> binaryvalue;
		binaryvalue.push_back('1');

		std::vector<unsigned char> sum;
		sum.push_back('0');

		std::cout << "mid bintodec" << std::endl;

		for (int i = (int) binrep.size() - 1; i >= 0; i--) {

			if (binrep.at(i) == '0') {
				//std::cout << std::endl;
				//std::cout << "0 yay" << std::endl;
				value = nextvalue(value);
			} else if (binrep.at(i) == '1') {
				///std::cout << std::endl;
				//std::cout << "1 yay" << std::endl;
				sum = myadd(sum, value);
				value = nextvalue(value);
				//std::cout << "sum: " << sum << std::endl;
			}
		}

		std::cout << "end bintodec" << std::endl;

		for (int i = 0; i < sum.size(); i++) {
			printf("%c", sum.at(i));
		}
		std::cout << std::endl;

		return sum;  // Everything is good, sum is correct
	}
Last edited on
1
2
3
4
		unsigned char* uc = new unsigned char(); //dynamic (¿?) allocation of just 1 character
		sin >> uc;

		for (int i = 0; i < strlen((char*) uc); i++) 
You reserve space for just 1 character, but it seems that you are trying to read a string.
Use std::string instead.
Actually that takes a hexadefimal string that can be an arbitary length. Can you explain why i would need to use std:string when i am storing bigintegers as a vector of unsigned char?
So you can read a string of arbitrary length.
Many thanks using the string solved the problem, curious to why it only caused issue when working with larger numbers
Actually that takes a hexadefimal string that can be an arbitary length.


Are we talking about the same thing? This:
unsigned char* uc = new unsigned char(); ?
Yes, it takes the stream and stores it no matter the size , though I feel my improper use of pointers is what cause the error.
Since there's only enough memory allocated for a single char, all the rest of it will be stored in other memory. If that other memory is being used to store other data, that other data will be trashed. If you are lucky, the program will crash and you will know you have trashed it. If you are unlucky, you won't know that you have trashed it. The more memory you trash, the greater the chance of trashing something important and finding out.

This is known as "buffer overrun".

A C++ string takes care of its own memory, so this doesn't happen with a C++ string.
Last edited on
Topic archived. No new replies allowed.