String limits?

  • Thread starter STEPHEN BECKER I V
  • Start date
S

STEPHEN BECKER I V

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
 
R

Robert Klemme

STEPHEN BECKER I V said:
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
 
C

Charles Mills

Memory is the limit (plus maybe some fixed max int that is allowed for
a
malloc() call).


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
 
M

Markus

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top