X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Trace-PostClient-IP: 68.147.131.211 From: Brian Inglis Newsgroups: comp.os.msdos.djgpp Subject: Re: strange comparison error Organization: Systematic Software Message-ID: References: X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Lines: 75 Date: Mon, 22 Mar 2004 20:09:08 GMT NNTP-Posting-Host: 24.71.223.147 X-Complaints-To: abuse AT shaw DOT ca X-Trace: pd7tw3no 1079986148 24.71.223.147 (Mon, 22 Mar 2004 13:09:08 MST) NNTP-Posting-Date: Mon, 22 Mar 2004 13:09:08 MST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Mon, 22 Mar 2004 17:54:51 +0100 in comp.os.msdos.djgpp, Rafael García wrote: >I have got an error message today that I cannot reproduce in my working >machine. I don't understand what could cause it but perhaps some of you can >give me an idea. > >I have something like this: >-------------------------------------------------------- >#define MAXFIELDS 5000 >... >t_field *pcampo,*reg; >int ncampo,relativo; >... >pcampo=reg+ncampo+relativo*nc; // for example remember C address arithmetic works in units of t_field: multiplies ncampo+relativo*nc by sizeof t_field before adding to reg; are you sure you need to multiply relativo by nc here? you are adding relativo * nc t_field objects; how big is nc here? >... >if (pcampo>reg+MAXFIELDS) { // intercept huge (bad) values multiplies MAXFIELDS by sizeof t_field before adding to reg > printf("Wrong field number: %d",(int)(pcampo-reg)); divides the difference between the addresses by sizeof t_field > return NULL; } >do something with *pcampo >--------------------------------------------------------- >Well, this is not complete but that is not the point because it doesn't >fail now. Nor it failed executing from RHIDE, when I tried to debug it with >a breakpoint. It only failed in the morning on a machine to which I cannot >get now. >The problem is at the 'if'. It gave "Wrong field number: 14" but I can't >understand how can it be 14 pcampo-reg and go into de "if (pcampo>reg+5000) >". I did solve it changing to "if (pcampo-reg>MAXFIELDS)", but cannot >understand de difference. should not be overflow, as scaled value of MAXFIELDS added should only be about 450000-480000, although using the subtraction ensures that the calculation works in units of t_field instead of bytes, so you're less likely to overflow; once again, maybe look at the value of nc to see if the earlier calculation of pcampo could have overflowed? >Perhaps that change didn't really solve it, but the mere recompilation >corrected something. I don't think so. > >Could you imagine something can affect this pointer arithmetic? This same >code is working regularly on other machines running many million times a >day. >If you think the definition of 't_field' could be important, here it is >(without translation). > >typedef struct { char *nombre; int tam; > char *def; int vdesp; int hdesp; char *buf; unsigned >adoptamoneda:1; #ifndef NOCACHEATR > BOOL sincacheatr; > int cacheatrlisto; > char *cacheatributos[NATRIBUTOSCACHEABLES]; >#endif > } t_campo; > > >NOCACHEATR is undefined >NATRIBUTOSCACHEABLES is something like 15 sizeof t_field/t_campo should be between 90-96 bytes, depending on whether adoptamoneda and sincacheatr are compiled as byte sized or word sized -- Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Brian DOT Inglis AT CSi DOT com (Brian dot Inglis at SystematicSw dot ab dot ca) fake address use address above to reply