[erlang-questions] process will stack overflow after received many message?

Steve Strong <>
Fri Sep 16 11:06:14 CEST 2011


 You might find this link useful as well http://en.wikipedia.org/wiki/Tail_call - the reason that your new example is stacking is that having code after the recursion means that it's no longer tail-recursive, and will indeed consume stack space.

-- Steve Strong
@srstrong

Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Friday, 16 September 2011 at 10:57, Jovi Zhang wrote:

> On Fri, Sep 16, 2011 at 4:44 PM, Robert Raschke < (mailto:)> wrote:
> > 
> > On Fri, Sep 16, 2011 at 9:38 AM, Jovi Zhang < (mailto:)> wrote:
> > > 
> > > Hi,
> > > I am a newbie of Erlang, here I have a question for receive
> > > message in Erlang loop.
> > > Like below Erlang code, when process receive one message, it will
> > > invoke loop again,
> > > then it can service like a server, receive message forever.
> > > BUT is possible that process will stack overflow after receive too
> > > many message? the process invoke loop again and again.
> > > 
> > > I saw there have many code write like this in <<Programming Erlang>>
> > > book.
> > > 
> > > loop() ->
> > > receive
> > > hello ->
> > > io:format("hello\n"),
> > > loop();
> > > Other ->
> > > io:format("I don't know what is this message, ~p is ~n"
> > > ,[Other]),
> > > loop()
> > > end.
> > 
> > No stack overflow, due to tail call optimisation. See also
> > http://www.erlang.org/doc/reference_manual/functions.html#id74170
> > 
> > Robby
> 
> Thanks, but when I test it using escript, the result is like below,
> "loop end" printed two times, it means it stacked!
> 
> [ Erlang]# cat jovi.erl
> #!/usr/bin/env escript
> 
> loop() ->
>  receive
>  one ->
>  io:format("one\n"),
>  loop();
>  two ->
>  io:format("two\n")
>  end,
>  io:format("loop end\n").
> 
> 
> main(_) ->
>  Pid = spawn(fun() -> loop() end),
>  Pid ! one,
>  Pid ! two,
>  receive
>  _Any -> void
>  end.
> 
> 
> [ Erlang]# ./jovi.erl
> one
> two
> loop end
> loop end
> ^C
> _______________________________________________
> erlang-questions mailing list
>  (mailto:)
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110916/ed8b4e7b/attachment.html>


More information about the erlang-questions mailing list