Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #54651

Re: C program. run time abend, range fault.

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: C program. run time abend, range fault.
Date 2014-12-16 10:31 +0000
Organization Freie Universitaet Berlin
Message-ID <cfag0nF9r44U1@mid.uni-berlin.de> (permalink)
References <3636bf65-fa27-43a6-b54f-45477b249b5d@googlegroups.com>

Show all headers | View raw


Shiva <subramanian.klu@gmail.com> wrote:
> There's an arithmetic program that I wrote in C. It was converted from
> fortran. For the same inputs the fortran code does not give out any
> run time abends but my C code does. No compilation errors, but run
> time abends as follows:

> Run time error: Range fault.
> In funcB.
> In funcC.
> In XXXX program.

> Pardon the weirdness in stating the run time error in such a way. But
> in the environment that I use, this is how it is blurted out. So my
> program has three functions and a main function - of which
> CALCZ-MATCH-FUNC and CALCZ-FUNC are two of them. Now I have complex
> exponential statements within these functions. Within the
> CALCX-MATCH-FUNC, CALCZ-FUNC is called once and that's when the range
> fault occurs as it is trying to do the following (1/10 ^ (-72)).

> I understand that this should produce a range fault as the resultant
> value goes out of bounds. But I want to catch this error from within
> the program and I don't want it to abend at run time. Any range fault
> or calculation overflow has to be catched because in run time the
> program should not fail. Can it redirect and set some flag or return
> some output when this happens so that the calling program doesn't have
> to abend as well, if this program abends.

> I strictly have to use just C in this environment. And I have the
> errors.h library header file. I'm not sure how to use it here.

Note that this 'erros.h' header file is not a standard C
header file and perhaps specific to the library. Since you
don't mention which library you're using it's hard to even
guess what this file might contain.

> The following is the code:

>     long long int funcA (long long int  pA, long long int pB,long long int pC,long long int pD, long long int pF, long long int pN)
>     {
>     ... Initialisation...
>     funcB (A, pF, pN, pD, pC, pB, pA, z);
>     ...initialisations value assigning to other variables...A

>     funcB (A, pF, pN, pD, pC, pB, pA, z);
>     do
>     {
>      funcC (A, pF, pN, pD, pC, pB, pA, v, w, x, y, z);
>      count = count + 1;
>      } while (((z >= 0.0001) || (z <= 0.0001)) && count < limit);

Here seems to be somthing very wrong. In C values are passed
to functions by value. That means that what funcB() and funcA()
receive e.g. for 'A' is the value 'A' has in the caller. Any
modifications these functions make to 'A' (or the other argu-
ments) are strictly local to these functions and un. Consider a very
simple program like this:

#include <stdio.h>

void foo( int x ) {
    x = 7;
}

int main( void ) {
    int a = 3;
    foo( a );
    printf( "%d\n", a );
    return 0;
}

The way your program seems to be written you probably will
expect that this program will print "7". But it doesn't, it
prints "3". That's because what the foo() function receives
is just the value 3, which gets stored in a variable named
'x' local to the foo() function. Modifying 'x' in foo() has
no effect at all on the value of 'a' in main()!

If you want to changed what's stored in 'a' by a call of
foo() you have to use a pointer. The program then would
look like this:

#include <stdio.h>

void foo( int * x ) {         /* function expects poiner to int */
    *x = 7;                   /* modify what's stored at that address */
}

int main( void ) {
    int a = 3;
    foo( &a );                /* pass a pointer to 'a' */
    printf( "%d\n", a );
    return 0;
}

Now foo() is able to modify what 'a' contains. That's because
it receives the address of 'a' (and not the value of what's
stored in 'a').

This is completely different from FORTRAN where everything is
passed by address (at least that's how it was when I learned
FORTRAN many years ago;-) Since the functions in FORTRAN
thus work directly on addresses all changes to arguments
made in a function also apply to the caller. But this isn't
how things work in C. In C you have to explicitely tell when
you want to pass an address instead of just a value, and you
have to explicitely assign to the address in the function!
(Well, arrays are a bit different, but since you don't seem
to use any I wont go into this...)

>      if ( count = limit)
>      A = -1;
>     else
>     {
>      ... Some more math..
>      }
>      return A;
>      }

>      void funcB (double A, int pF, int pN, int pD, double pC, double pB, double pA, double z)
>      {
>      z = (pow (A, pF) * (A - 1)) / (1 - (pow (A, -pN)));
>      z = pB / z;
>      z = z - pA - (pC / pow (A, pD));
>      }

>      void funcC (double A, int pF, int pN, int pD, int pC, double pB, double pa, double v, double w, double x, double y, double z)

>      {
>      A = some math...
>      calcz (A, pF, pN, pD, PC, pB, pA, z);
>      Some more math...
>      }

> The funcA is called by a Cobol program, and this C program compiles
> fine. But only during run time there's an error. The input is just 6
> values. pA, pB, pC, pD, pF, pN.

> If you see in funcC it calls funcB whose first line of code does a
> complex exponential calculation which overflows at some particular
> iteration, resulting in the range fault like I stated earlier.

It eems to me rather likely it;s due to the fact that all
the results computed in funcB() and funcC() are thrown away
the moment you leave these functions instead of appearing in
the caller. If you need them in the caller you have to either
return a value as the return value of the function (funcB()
looks like a candidate for that since you only seem to want
'z' as a result) or, if you need to get several values back
from a function call, by using pointers (a different way to
avoid the use of pointers would be the use of a structure as
a return value of the function).

> Inputs:

>       pA = 1000 pB = 44.50 pN = 25 pC = 0 pD = 2 pF = 2

> These are correct inputs for which program works fine. Only for a few
>  weird inputs passed, like below, it gives the range fault. 

>       pA = 1000
>       pB = 144.50
>       pN = 25
>       pC = 0
>       pD = 2
>       pF = 2

> I just need to catch this error in run time and tell the calling
> program that this is a bad input. I don't want the program to fail.
> Because it would cause the calling program to fail as well. Could this
> be accomplished in C itself?

Sorry, but you'll have to fix the primary problem first.
And then there's hardly any information on what really
happens in the program (pr what library you're using), so
it is impossible to tell why you get some "range fault"
under certain circumstances (and what's throwing it) as
well as how to avoid it.
                             Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-16 00:47 -0800
  Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-16 10:31 +0000
    Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-16 11:33 -0800
      Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-16 14:27 -0800
      Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-16 23:08 +0000
      Re: C program. run time abend, range fault. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-16 23:44 +0000
    Re: C program. run time abend, range fault. glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-16 19:37 +0000
      Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-17 00:12 +0000
        Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-17 13:05 -0800
          Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-17 15:06 -0800
            Re: C program. run time abend, range fault. Richard Heathfield <invalid@see.sig.invalid> - 2014-12-17 23:15 +0000
              Re: C program. run time abend, range fault. Keith Thompson <kst-u@mib.org> - 2014-12-17 16:00 -0800
            Re: C program. run time abend, range fault. Keith Thompson <kst-u@mib.org> - 2014-12-17 15:56 -0800
            Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 12:28 -0800
              Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-18 14:07 -0800
              Re: C program. run time abend, range fault. Barry Schwarz <schwarzb@dqel.com> - 2014-12-18 14:16 -0800
              Re: C program. run time abend, range fault. jacob navia <jacob@spamsink.net> - 2014-12-19 00:55 +0100
              Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-19 00:18 +0000
              Re: C program. run time abend, range fault. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-19 01:12 +0000
          Re: C program. run time abend, range fault. jt@toerring.de (Jens Thoms Toerring) - 2014-12-18 01:00 +0000
          Re: C program. run time abend, range fault. James Kuyper <jameskuyper@verizon.net> - 2014-12-17 22:29 -0500
            Re: C program. run time abend, range fault. gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-18 09:18 +0000
              Re: C program. run time abend, range fault. Martin Shobe <martin.shobe@yahoo.com> - 2014-12-18 11:10 -0600
                Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 11:38 -0800
                Re: C program. run time abend, range fault. James Kuyper <jameskuyper@verizon.net> - 2014-12-18 15:24 -0500
                Re: C program. run time abend, range fault. Shiva <subramanian.klu@gmail.com> - 2014-12-18 12:37 -0800
  C program. run time abend, range fault. asetofsymbols@gmail.com - 2014-12-18 22:21 -0800
  Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2014-12-19 18:32 +0100
    Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2014-12-20 11:34 +0100
      Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2014-12-31 11:48 -0800
        Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2014-12-31 15:49 -0500
        Re: C program. run time abend, range fault. Is this the correct solution? Keith Thompson <kst-u@mib.org> - 2014-12-31 13:07 -0800
          Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2014-12-31 13:57 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? Keith Thompson <kst-u@mib.org> - 2014-12-31 16:02 -0800
              Re: C program. run time abend, range fault. Is this the correct solution? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-31 22:55 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 14:56 +0000
        Re: C program. run time abend, range fault. Is this the correct solution? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-31 21:51 +0000
        Re: C program. run time abend, range fault. Is this the correct solution? jacob navia <jacob@spamsink.net> - 2015-01-01 02:51 +0100
          Re: C program. run time abend, range fault. Is this the correct solution? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 15:00 +0000
            Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-05 09:45 -0800
              Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2015-01-05 11:02 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-06 10:33 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2015-01-06 14:09 -0500
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-06 12:32 -0800
                Re: C program. run time abend, range fault. Is this the correct solution? James Kuyper <jameskuyper@verizon.net> - 2015-01-06 15:47 -0500
                Re: C program. run time abend, range fault. Is this the correct solution? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-06 23:58 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-07 04:31 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? Robert Wessel <robertwessel2@yahoo.com> - 2015-01-06 23:36 -0600
                Re: C program. run time abend, range fault. Is this the correct solution? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 07:08 +0000
                Re: C program. run time abend, range fault. Is this the correct solution? Shiva <subramanian.klu@gmail.com> - 2015-01-09 13:46 -0800
            Re: C program. run time abend, range fault. Is this the correct solution? Geoff <geoff@invalid.invalid> - 2015-01-05 09:56 -0800
  Re: C program. run time abend, range fault. Geoff <geoff@invalid.invalid> - 2015-01-01 14:38 -0800

csiph-web