473,569 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with C project

My hobby project is a library of routines in C that do big integer
arithmetic. I've been working on it for several weeks and it now
consists of about 1200 lines of code. I've got addition, subraction and
multiplication working, but there is a problem with division. I think my
algorithm is OK; it's nothing fancy, just grade school long division. It
crashes with a seg fault on some inputs and produces the correct result
with others. Despite putting in a great deal of time trying to find this
bug, I've been unsuccessful.

I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer out
there who would be willing to take a look at the code (its on my web
site). I know it's asking a lot and I have nothing to offer in return
except my gratitude.

If you are interested, please email me.

saltonpepper
at
shaw
dot
ca

Marlene Stebbins
Nov 14 '05 #1
6 1441
Marlene Stebbins wrote:
My hobby project is a library of routines in C that do big integer
arithmetic. I've been working on it for several weeks and it now
consists of about 1200 lines of code. I've got addition, subraction and
multiplication working, but there is a problem with division. I think my
algorithm is OK; it's nothing fancy, just grade school long division. It
crashes with a seg fault on some inputs and produces the correct result
with others. Despite putting in a great deal of time trying to find this
bug, I've been unsuccessful.

I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer out
there who would be willing to take a look at the code (its on my web
site). I know it's asking a lot and I have nothing to offer in return
except my gratitude.

If you are interested, please email me.

saltonpepper
at
shaw
dot
ca

Marlene Stebbins


Check out some of the Big Number libraries, such as:
http://www.openssl.org/docs/crypto/bn.html#

You can download the source. It should give you some
guidance.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #2
Marlene Stebbins wrote:
.... snip ...
I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer
out there who would be willing to take a look at the code (its on my
web site). I know it's asking a lot and I have nothing to offer in
return except my gratitude.


Which URL is conspicuously missing from your post.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #3
CBFalconer wrote:
Marlene Stebbins wrote:

... snip ...
I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer
out there who would be willing to take a look at the code (its on my
web site). I know it's asking a lot and I have nothing to offer in
return except my gratitude.

Which URL is conspicuously missing from your post.


Here 'tis:

http://members.shaw.ca/bystander/bignum.html
Nov 14 '05 #4
Marlene Stebbins wrote:
CBFalconer wrote:
Marlene Stebbins wrote:

... snip ...
I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer
out there who would be willing to take a look at the code (its on my
web site). I know it's asking a lot and I have nothing to offer in
return except my gratitude.


Which URL is conspicuously missing from your post.


Here 'tis:

http://members.shaw.ca/bystander/bignum.html


Um, it would have been nicer to get either links to the four
source files or a .zip archive containing all of them.

Apart from that: On which inputs does your routine fail?

I just looked over your code and found some problems but I
will not go through it without knowing the right inputs:

Your low-level functions are visible everywhere; writing, say
"static void abs_add (....)" effectively prevents users from
using routines which are only yours.

bignum.h:
You need not restrict yourself to ASCII or whatever, instead
of ZERO you can always write '0', instead of NINE you always
can write '9'. If you like your defines better, then at least
#define ZERO '0' and #define NINE '9'.
Your sign control can cause problems down the road, I would
rather use
enum {NEG=-1,POS=1};
or
enum {NEG=-1,NONE,POS};
This will also simplify sign calculation for your bigints
to a.sign*b.sign

bignum.c:
bigAlloc(): You can also use realloc()
int2big(): You multiply n with -1 without checking whether
n<-INT_MAX; for 2s complement machines, INT_MIN= -INT_MAX-1,
so you will run into undefined behaviour.
BTW: If you just malloc() a large enough string, you can
sprintf() n there and just call your ordinary str2big() (or
however it is called). After return, you free() the string
buffer.
big2int(): You do not check whether the return value of strtol()
makes sense but just cast it which can invoke UB.
I'd rather do the conversion by hand (checking against
INT_MAX (or INT_MAX/10)) or use strtof() and check against
INT_MAX.
bigCmp(): I would first evaluate the signs. As far as I have
understood, .size contains strlen(.num), so you do not need
the strlen() round here.
The test could also be (for sign in NEG, NONE, POS)
switch (a.sign*b.sign) {
case NEG:
return a.sign;
case NONE:
if (a.sign) return a.sign;
else return -b.sign;
case POS:
if (a.size > b.size) return a.sign;
else if (b.size > a.size) return -b.sign;
else return strcmp(a.number , b.number);
default:
assert(0);
return 0;
}
Note the default. You have no "default" in your tests.
mul_sign(), div_sign(): with (NEG,NONE,POS), this becomes
return dvd.sign*div.si gn;
or the equivalent for multiplication.
slice(): Are you sure about the first test?
You fail to check lo<=hi.
ascii2str(): You are just duplicating the string; the name
is misleading.

Further down: The
zero:;
label needs no semicolon as it is no statement.
I would mark the label by writing it at the same indentation
level as the surrounding block or at the very beginning of
the line:
zero:
pprod.sign....

After that I stopped.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #5
Michael Mair wrote:
Marlene Stebbins wrote:
CBFalconer wrote:
Marlene Stebbins wrote:

... snip ...

I hate to come this far and then fail in the very last phase of the
project. I'm wondering if there might be an experienced C programmer
out there who would be willing to take a look at the code (its on my
web site). I know it's asking a lot and I have nothing to offer in
return except my gratitude.
Which URL is conspicuously missing from your post.

Here 'tis:

http://members.shaw.ca/bystander/bignum.html

Um, it would have been nicer to get either links to the four
source files or a .zip archive containing all of them.

Apart from that: On which inputs does your routine fail?

I just looked over your code and found some problems but I
will not go through it without knowing the right inputs:


When tested with the rudimentary driver at the bottom of the file,
division fails with a seg fault when the divisor (argv[2]) is relatively
large (more than 8-10 digits). The size of the dividend doesn't seem to
matter; that is, if the dividend is very large and the divisor is small,
the correct result is output. Thanks for the rest of your comments. Your
points are well taken. I know the code still has lots of rough edges.
There doesn't seem to be much point in fixing them unless I can get
division working.

Marlene
Nov 14 '05 #6
Marlene Stebbins wrote:
Michael Mair wrote:
Marlene Stebbins wrote:
CBFalconer wrote:

Marlene Stebbins wrote:

... snip ...

> I hate to come this far and then fail in the very last phase of the
> project. I'm wondering if there might be an experienced C programmer
> out there who would be willing to take a look at the code (its on my
> web site). I know it's asking a lot and I have nothing to offer in
> return except my gratitude.

Which URL is conspicuously missing from your post.

Here 'tis:

http://members.shaw.ca/bystander/bignum.html
Um, it would have been nicer to get either links to the four
source files or a .zip archive containing all of them.

Apart from that: On which inputs does your routine fail?

I just looked over your code and found some problems but I
will not go through it without knowing the right inputs:

When tested with the rudimentary driver at the bottom of the file,
division fails with a seg fault when the divisor (argv[2]) is relatively
large (more than 8-10 digits). The size of the dividend doesn't seem to
matter; that is, if the dividend is very large and the divisor is small,
the correct result is output. Thanks for the rest of your comments. Your
points are well taken. I know the code still has lots of rough edges.
There doesn't seem to be much point in fixing them unless I can get
division working.


Understandable. .. but misleading.

I think I have found the _reason_ for your error:
void strip_zeroes(ch ar *s)
{
char *stripped;
int idx, jdx, zero_count, len;

zero_count = 0;
idx = 0;
len = strlen(s) + 1;

while(s[idx] == '0')
{
zero_count++;
idx++;
}
if((stripped = malloc((len-zero_count) * sizeof(*strippe d)))==NULL)
{
fprintf(stderr, "malloc failed in strip_zeroes\n" );
exit(EXIT_FAILU RE);
}
for(idx = zero_count, jdx = 0; idx < len; idx++, jdx++)
{
stripped[jdx] = s[idx];
}
if((s = realloc(s, (len - zero_count) * sizeof(*s)))==N ULL)
My debugger burped at this statement.
One thing: This is the completely _wrong_ way to handle realloc()
See below for the right way. If realloc() returns NULL, then your
buffer which s points to still exists at the address s.
By assigning NULL to s, you throw it away. However, this should
not cause a segmentation fault.
{
fprintf(stderr, "realloc failed in strip_zeroes()\ n");
exit(EXIT_FAILU RE);
}

strcpy(s, stripped);
free(stripped);
return;
}


However, it made me think about what you _want_ the function to
do. You pass a string and want to have _this_ string changed.
realloc() means that the address of the storage may change.
In order to bring that back into the pointer the value of which
we passed, we need the pointer's address:

void strip_zeroes(ch ar **s)
{
char *tmp;
size_t zero_count, len;

len = strlen(*s) + 1;

zero_count = strspn(*s, "0");
assert(zero_cou nt < len);
memmove(*s, *s+zero_count, len-zero_count);
if((tmp = realloc(*s, (len - zero_count) * sizeof *tmp))==NULL)
{
fprintf(stderr, "realloc failed in strip_zeroes()\ n");
exit(EXIT_FAILU RE);
}
*s = tmp;
return;
}

Now, you have to pass &mybignum.numbe r (and change the prototype
in the header).
Of course, as this was not the reason why the program crashed,
the pointer s we got passed was probably invalid in the first
place! So, the task is to look for a similar logical error which
happened to the argument "difference " of the function abs_sub.

Your turn :-)
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2077
by: Mark | last post by:
Hi, For the last 2 years I've been developing vehicle tracking/telemetric software for a company as a self employed individual. The project is quiet big, and is going to be there flagship product. Since I had no other work at the time, I agreed to a low rate when I first started which was £100 a day. The project started small, so it was...
3
2721
by: Andrej Hristoliubov | last post by:
I am the best c++ programmer in the whole wide world. Trust ME! My reference is Victor Bazarov,Valentin Samko,Alf P.Steinbach( Me and Alf actually intern together at Microsoft), and Bjarne Stroustrup (he actually asked me for help to design C++ ; still unsure why he didn't give me enought credit - probably a poor design choice to have...
2
1905
by: Susan Bricker | last post by:
Greetings Experts ... I have a routine that is invoked when a command button is clicked. The button is located in a form that is tied to a table of Project records. Each Project Record has 0 to many Task Records associated with it (in another table). I want to 'logically remove' the Project Record and all Task Records for the Project (by...
10
2861
by: Tom | last post by:
I am looking for some ideas for how to design the layout of the form for data entry and to display the data for the following situation: There are many sales associates. A sales associate can work for multiple companies and work for multiple divisions within each company. Within each division he can work in multiple departments and within each...
48
3204
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120...
6
1413
by: sivashankar | last post by:
hello experts -can any one give me some module to do in asp.net -if u give me the description i will give u with free of cost,since am new now am in my final semester of MCA,just give me some time. -Can any one give me some project to do -If so i will be greatfull to u Thanks In Advance Siva
7
1441
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4 Idea = 5
5
5088
by: info | last post by:
Hi! Please be patient with a newbie... I use DevC++. I've found and compiled succefully various openGL examples. Before build the application, I link the project with static libs, like, in example, libglut32.a. The executables running fine, but if I try them on a system without glut32.dll, they don't start.
8
3481
by: Brett Romero | last post by:
I have this situation: myEXE <needs< DerivedClass <which needs< BaseClass Meaning, myEXE is using a type defined in DerivedClass, which inherits from BaseClass. I include a reference to DerivedClass in myEXE, which gives this error: The type 'BaseAssembly.SomeClass' is defined in an assembly that is not referenced. You must add a...
10
2086
by: Frank | last post by:
I've done this a few times. In a solution I have a project, Say P1, and need another project that will contain much code that is similar to that of P1. I hope no one gets hung up on why I don't somehow share the code. So, I copy the folder P1 is in, change the new folder name, and is VS2005 to change all occurrences of P1's name tp...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.