caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Crash reading a file
@ 2008-02-04 14:54 tmp123
  2008-02-04 15:07 ` [Caml-list] " Adam Chlipala
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: tmp123 @ 2008-02-04 14:54 UTC (permalink / raw)
  To: caml-list

Hello,

The following program (foo.ml):

value rec read_stdin () =
  try
    [ (input_line Pervasives.stdin) :: (read_stdin ()) ]
  with [ End_of_file -> [] ];

value main () =
  read_stdin();

main();


when compiled with "ocamlopt -pp camlp4r foo.ml" and executed "./a.out", 
produces a segmentation fault, but I'm not able to find why.

Please, has someone any sugestion?

Thanks a lot.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Crash reading a file
  2008-02-04 14:54 Crash reading a file tmp123
@ 2008-02-04 15:07 ` Adam Chlipala
  2008-02-04 15:18 ` Chet Murthy
       [not found] ` <6f9f8f4a0802040719u6b47cef2n3c3d41b382964773@mail.gmail.com>
  2 siblings, 0 replies; 6+ messages in thread
From: Adam Chlipala @ 2008-02-04 15:07 UTC (permalink / raw)
  To: tmp123; +Cc: caml-list

Your function isn't tail recursive, so it uses stack space proportional 
to the number of lines in the file.  "Segmentation fault" may be your 
architecture's "stack overflow" error.

tmp123 wrote:
> The following program (foo.ml):
>
> value rec read_stdin () =
>  try
>    [ (input_line Pervasives.stdin) :: (read_stdin ()) ]
>  with [ End_of_file -> [] ];
>
> value main () =
>  read_stdin();
>
> main();
>
>
> when compiled with "ocamlopt -pp camlp4r foo.ml" and executed 
> "./a.out", produces a segmentation fault, but I'm not able to find why.
>
> Please, has someone any sugestion? 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Crash reading a file
  2008-02-04 14:54 Crash reading a file tmp123
  2008-02-04 15:07 ` [Caml-list] " Adam Chlipala
@ 2008-02-04 15:18 ` Chet Murthy
       [not found] ` <6f9f8f4a0802040719u6b47cef2n3c3d41b382964773@mail.gmail.com>
  2 siblings, 0 replies; 6+ messages in thread
From: Chet Murthy @ 2008-02-04 15:18 UTC (permalink / raw)
  To: caml-list; +Cc: tmp123

Besides Adam's correct obervation that you're recursing in a non-tail
manner, note also that Ocaml doesn't guarantee any specific
order-of-evaluation, wihch means that the recursive call in the cons
-could- be evaluated before the input_line.  In fact, since the
-current- implementation -does- evaluate in right-to-left order, that
-is- what's going to happen.

# let f x = Printf.fprintf stderr "%s\n" x; flush stderr; x;;
val f : string -> string = <fun>
# let l = ((f"a")::(f"b")::[]);;
b
a
val l : string list = ["a"; "b"]

My guess is, you're getting infinite stack-recursion for even empty
files.  Make your order of evaluation explicit, and you'll only get it
for large files.

--chet--

On Monday 04 February 2008 06:54, tmp123 wrote:
> Hello,
>
> The following program (foo.ml):
>
> value rec read_stdin () =
>   try
>     [ (input_line Pervasives.stdin) :: (read_stdin ()) ]
>   with [ End_of_file -> [] ];
>
> value main () =
>   read_stdin();
>
> main();
>
>
> when compiled with "ocamlopt -pp camlp4r foo.ml" and executed "./a.out",
> produces a segmentation fault, but I'm not able to find why.
>
> Please, has someone any sugestion?
>
> Thanks a lot.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Crash reading a file
       [not found] ` <6f9f8f4a0802040719u6b47cef2n3c3d41b382964773@mail.gmail.com>
@ 2008-02-04 16:21   ` tmp123
  2008-02-04 16:26     ` Ashish Agarwal
  0 siblings, 1 reply; 6+ messages in thread
From: tmp123 @ 2008-02-04 16:21 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/html, Size: 1732 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Crash reading a file
  2008-02-04 16:21   ` tmp123
@ 2008-02-04 16:26     ` Ashish Agarwal
  2008-02-04 18:22       ` Loup Vaillant
  0 siblings, 1 reply; 6+ messages in thread
From: Ashish Agarwal @ 2008-02-04 16:26 UTC (permalink / raw)
  To: caml-list, tmp123

[-- Attachment #1: Type: text/plain, Size: 237 bytes --]

> but then I must append at the end of a list, that is something time
consuming. I do not known about more options.

Append to the beginning of the list during the recursion, producing the list
in reverse. Then do a List.rev at the end.

[-- Attachment #2: Type: text/html, Size: 255 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Crash reading a file
  2008-02-04 16:26     ` Ashish Agarwal
@ 2008-02-04 18:22       ` Loup Vaillant
  0 siblings, 0 replies; 6+ messages in thread
From: Loup Vaillant @ 2008-02-04 18:22 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: caml-list, tmp123

2008/2/4, Ashish Agarwal <agarwal1975@gmail.com>:
> > but then I must append at the end of a list, that is something time
> consuming. I do not known about more options.
>
> Append to the beginning of the list during the recursion, producing the list
> in reverse. Then do a List.rev at the end.

If you don't need persistence, you can also use a stream. That way,
the stream will be consumed only as needed, and you'll have a constant
space usage, (both heap and stack).


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-02-04 18:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-04 14:54 Crash reading a file tmp123
2008-02-04 15:07 ` [Caml-list] " Adam Chlipala
2008-02-04 15:18 ` Chet Murthy
     [not found] ` <6f9f8f4a0802040719u6b47cef2n3c3d41b382964773@mail.gmail.com>
2008-02-04 16:21   ` tmp123
2008-02-04 16:26     ` Ashish Agarwal
2008-02-04 18:22       ` Loup Vaillant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).