[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String limits?

STEPHEN BECKER I V

10/5/2004 2:18:00 PM

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
and its a c :) but does ruby actively prevent overflow? am i thinking
about this in the wrong way?
Stephen


3 Answers

Robert Klemme

10/5/2004 2:51:00 PM

0


"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*).

Kind regards

robert

Charles Mills

10/5/2004 6:16:00 PM

0

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



Markus

10/5/2004 7:08:00 PM

0

On Tue, 2004-10-05 at 11:16, Charles Mills wrote:
> 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).

Of course, the actual memory in your machine (even including swap
space, etc.) can be orders of magnitude less than the "addressable
memory size" on your machine.

-- Markus