Charles Mills
10/5/2004 6:16:00 PM
On Oct 5, 2004, at 7:54 AM, Robert Klemme wrote:
>
> "STEPHEN BECKER I V" <Becker004@gmail.com> schrieb im Newsbeitrag
> news:3703ec2d04100507177d3aee72@mail.gmail.com...
>> Does a string have a limited size? assuming running on the average
> desktop.
>>
>> i know
>> var="a"*1000000
>> print var
>> prints 1million a's, i even try declaring the values around it
>> var0="a"
>> var1="b"
>> var2="c"
>> var1=var1*1000000
>> print var2
>
> Memory is the limit (plus maybe some fixed max int that is allowed for
> a
> malloc() call).
>
>> and its a c :) but does ruby actively prevent overflow? am i thinking
>> about this in the wrong way?
>> Stephen
>
> In case of failure you'll see somethig like this:
>
> /usr/lib/ruby/1.8/irb.rb:296:in `inspect': failed to allocate memory
> (NoMemoryError)
> from /usr/lib/ruby/1.8/irb.rb:296:in `output_value'
> ....
>
> Normally you don't have to worry at all about overflow problems. There
> are no buffer overflows possible with Ruby strings as there are with C
> strings (well, C doesn't really have strings - it just has char*).
Each Ruby String's length is stored in a long and Ruby won't compile
unless sizeof(long) == sizeof(void*) so Ruby Strings can't be bigger
than half the addressable memory size on your machine, since usually
LONG_MAX == ULONG_MAX / 2
and void* can address up to ULONG_MAX (on machines ruby compiles on).
-Charlie