binaries vs lists

Bengt Kleberg Bengt.Kleberg@REDACTED
Tue Nov 11 16:44:05 CET 2003


Serge Aleynikov wrote:
> Bengt Kleberg wrote:
> 
>> Serge Aleynikov wrote:
>>
...deleted
>>> I have an Erlang TCP client that processes a binary stream which 
>>> needs to be post-processed by removing escaped bytes.  Let's say, 
>>> that byte 16$FF is escaped as <<16#FE, 16$01>>, and the 16#FF value 
>>> is used as a message separator.  The variable hex messages are within 
>>> 512 bytes each.
>>>
>>
>> if i understood the 512 bytes statement correctly, it is ok to build 
>> the list on the stack, thus avoiding lists:reverse/1 in solution 1.
> 
> 
> Do I have a control over where the list is being built (heap/stack)?  Or 
> this is purely dependent on the list size?  If so, are you implying that 
> I should append items at the end instead of doing it in front, and using 
> lists:reverse/1 ?
> 
if you build a list in an accumulator this is (usually :-) done using 
heap space:
fn([], Acc) ->
	lists:reverse(Acc);
fn([H | T], Acc) ->
	fn(T, [H | Acc]).


if you build a list using 'cons' this is ''usually'' done using stack space:
fn([]) ->
	[];
fn([H | T]) ->
	[H | fn(T)]).

you can crash your process if it uses too much space. stack space is 
usually smaller than heap space. however, in this case you seemed to say 
that there was a maximum of 512 bytes to a list. which usually is 
available on your stack.


bengt




More information about the erlang-questions mailing list