English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
small code problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-07-08 (23:46)
From: Gerd Stolpmann <Gerd.Stolpmann@d...>
Subject: Re: small code problem
On Sat, 03 Jul 1999, Brad Knotwell wrote:
>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]$	

On an x86 architecture I get a different result:
Fatal error: uncaught exception Invalid_argument("out-of-bound array or string access")

This means that there are two problems:
- The segfault
- The different exceptions raised by the bytecode and the native compiler.

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

let usage() = Printf.printf "Usage: %s file\n" Sys.argv.(0); exit ~-1;;
let filename =
	match Sys.argv with
 		[| |]   -> failwith "You have a silly operating system"
	      | [| _ |] -> usage()
	      | _       -> Sys.argv.(1)

This solution avoids that Invalid_argument must be caught which is better
style. It is almost always a bad idea to trap Invalid_argument, 
Out_of_memory, Stack_overflow, and Division_by_zero, as these exceptions
are thought as last resort that indicates a bad system state that must
not happen but happened. It is better to design the program such that
it never runs into conditions that raise these exceptions, and if they
happen nevertheless, you missed something.

Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   Gerd.Stolpmann@darmstadt.netsurf.de (privat)