Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* small code problem
@ 1999-07-02 22:30 <Brad Knotwell
  1999-07-08  4:56 ` Markus Mottl
  1999-07-08 22:49 ` Gerd Stolpmann
  0 siblings, 2 replies; 8+ messages in thread
From: <Brad Knotwell @ 1999-07-02 22:30 UTC (permalink / raw)
  To: caml-list


Hello all--

I'm having a problem with a core dump due to an uncaught exception.
The following snippet of code best illustrates the problem:

let usage = lazy (Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1);;
let filename =
        try
           Sys.argv.(1)
        with Invalid_argument("Array.get") ->
           Lazy.force usage;;

When I compile using the byte-code compiler, it seems to work fine.

         [knotwell@knotwell stock]$ ocamlc junk.ml
	 [knotwell@knotwell stock]$ ./a.out
	 Usage: ./a.out file
	 [knotwell@knotwell stock]$ 

On the other hand, when I compile with the optimizing compiler, I
receive the following:

	[knotwell@knotwell stock]$ ocamlopt junk.ml
	[knotwell@knotwell stock]$ ./a.out
	Segmentation fault (core dumped)
        [knotwell@knotwell stock]$	
When I change the code slightly:


let usage = lazy (Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1);;
let filename =
        try
           Sys.argv.(1)
        with _ ->          (* note the pattern matching change *)
           Lazy.force usage;;

I then get correct results:

       [knotwell@knotwell stock]$ ocamlopt junk.ml
       [knotwell@knotwell stock]$ a.out
       Usage: a.out file
       [knotwell@knotwell stock]$	

Why do the two compilers exhibit different behaviors with the same code?

As an aside, I don't particular like my use of lazy and force.  Is
there a cleaner way do the same thing?

Thanks.

--Brad




^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re:  small code problem
@ 1999-07-08 18:23 Damien Doligez
  0 siblings, 0 replies; 8+ messages in thread
From: Damien Doligez @ 1999-07-08 18:23 UTC (permalink / raw)
  To: caml-list

>From: "<Brad Knotwell" <knotwell@f5.com>

>let usage = lazy (Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1);;
>let filename =
>        try
>           Sys.argv.(1)
>        with Invalid_argument("Array.get") ->
>           Lazy.force usage;;

>         [knotwell@knotwell stock]$ ./a.out
>         Usage: ./a.out file

The doc says Invalid_argument should be called, but it doesn't say
anything about the string in the exception.  On my alpha with the
current development version of O'Caml, this is "Array.get" for the
byte-code interpreter (which gives the expected result), but
"out-of-bound array or string access" with the native-code compiler.

The bug with uncaught exceptions seems to be architecture-specific, so
you should tell us what kind of machine and system you have.


>As an aside, I don't particular like my use of lazy and force.  Is
>there a cleaner way do the same thing?

Certainly.  Just write:

  let usage () = Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1;;
  let filename =
          try
             Sys.argv.(1)
          with Invalid_argument(_) ->
             usage ();;

Better yet, use the Arg module to parse your command line...

-- Damien




^ permalink raw reply	[flat|nested] 8+ messages in thread
[parent not found: <199907090927.KAA09195@toy.william.bogus>]

end of thread, other threads:[~1999-07-12 18:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-02 22:30 small code problem <Brad Knotwell
1999-07-08  4:56 ` Markus Mottl
1999-07-08 22:49 ` Gerd Stolpmann
1999-07-09  0:27   ` <Brad Knotwell
1999-07-09  1:37     ` Fabrice Le Fessant
1999-07-08 18:23 Damien Doligez
     [not found] <199907090927.KAA09195@toy.william.bogus>
1999-07-09 13:56 ` Gerd Stolpmann
1999-07-11 19:23   ` Markus Mottl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox