473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1428
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.learn.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********@yahoo.com) (cb********@worldnet.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.sign;
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(char *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(*stripped)))==NULL)
{
fprintf(stderr, "malloc failed in strip_zeroes\n");
exit(EXIT_FAILURE);
}
for(idx = zero_count, jdx = 0; idx < len; idx++, jdx++)
{
stripped[jdx] = s[idx];
}
if((s = realloc(s, (len - zero_count) * sizeof(*s)))==NULL)
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_FAILURE);
}

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(char **s)
{
char *tmp;
size_t zero_count, len;

len = strlen(*s) + 1;

zero_count = strspn(*s, "0");
assert(zero_count < 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_FAILURE);
}
*s = tmp;
return;
}

Now, you have to pass &mybignum.number (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
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...
3
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...
2
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...
10
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...
48
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...
6
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...
7
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...
5
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...
8
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...
10
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.