M
Martin the Third
Hi, I need some help!
I'm writing an infinite-precision floating point library called
ipfloat (I know infinite is a misnomer - but arbitrary was taken). A
quick overview: I'm storing numbers as a char* mantissa, an int
exponent and a sign. For example, the number "123.45" would have a
mantissa of 12345, an exponent of 2, and a sign of +. I'm essentially
storing it as "1.2345x10^2".
The mantissa is allocated dynamically at runtime to allow arbitrary
precision in my numbers. I'm having trouble with heap overflows; let
me explain in more detail:
I'm trying to write a += or + operator overload. Originally I wrote a
+= function that simply expanded the mantissa of the left operand to
hold the entire result, and then return itself. This causes a heap
overflow, for example, with the following numbers:
0.0000000000000000001 + 13123123123.123123123
Here, the mantissa of the left operand is "1" but the right is
"13123123123123123123". When the memory is allocated, the left
mantissa is allocated, then the right one is allocated, so their
addresses are very close - the left operand has a tiny mantissa. When
I expand that mantissa to hold the entire result, this obviously
overflows into the right operand's mantissa, and I either get really
funky answers or a heap overflow and a segfault.
The solution, I thought, was to write a + operator overload instead.
Then, I'd have a left and right operand, but I wouldn't touch them at
all - instead I'd create a local "ipfloat result" and then store the
answer in result, and return result. This works!
ipfloat a, b;
a+b; //no errors!
But...when used in a real situation:
ipfloat a(1232.12312);
ipfloat b(123123.231231);
ipfloat c;
c = a+b;
There are, once again, heap overflows causing corruption. Here, the
ipfloat result that I'm returning from the addition is trying to be
stored in c, but c was allocated _before_ the operation took place and
so before result was created. Then the assignment operator tries to
expand c (which has a size of 2 - "0\0") to hold the whole answer, and
overwrites data in result, so again I have corruption, or overflow (or
both).
The only solution I can think of is to use memmove() in my assignment
operator. Is that viable? I'd really appreciate any help with how to
restructure this so that I can make it stable.
Thank you! I'll gladly post code if it will help.
I'm writing an infinite-precision floating point library called
ipfloat (I know infinite is a misnomer - but arbitrary was taken). A
quick overview: I'm storing numbers as a char* mantissa, an int
exponent and a sign. For example, the number "123.45" would have a
mantissa of 12345, an exponent of 2, and a sign of +. I'm essentially
storing it as "1.2345x10^2".
The mantissa is allocated dynamically at runtime to allow arbitrary
precision in my numbers. I'm having trouble with heap overflows; let
me explain in more detail:
I'm trying to write a += or + operator overload. Originally I wrote a
+= function that simply expanded the mantissa of the left operand to
hold the entire result, and then return itself. This causes a heap
overflow, for example, with the following numbers:
0.0000000000000000001 + 13123123123.123123123
Here, the mantissa of the left operand is "1" but the right is
"13123123123123123123". When the memory is allocated, the left
mantissa is allocated, then the right one is allocated, so their
addresses are very close - the left operand has a tiny mantissa. When
I expand that mantissa to hold the entire result, this obviously
overflows into the right operand's mantissa, and I either get really
funky answers or a heap overflow and a segfault.
The solution, I thought, was to write a + operator overload instead.
Then, I'd have a left and right operand, but I wouldn't touch them at
all - instead I'd create a local "ipfloat result" and then store the
answer in result, and return result. This works!
ipfloat a, b;
a+b; //no errors!
But...when used in a real situation:
ipfloat a(1232.12312);
ipfloat b(123123.231231);
ipfloat c;
c = a+b;
There are, once again, heap overflows causing corruption. Here, the
ipfloat result that I'm returning from the addition is trying to be
stored in c, but c was allocated _before_ the operation took place and
so before result was created. Then the assignment operator tries to
expand c (which has a size of 2 - "0\0") to hold the whole answer, and
overwrites data in result, so again I have corruption, or overflow (or
both).
The only solution I can think of is to use memmove() in my assignment
operator. Is that viable? I'd really appreciate any help with how to
restructure this so that I can make it stable.
Thank you! I'll gladly post code if it will help.