[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Ruby :symbols and C *pointers are related?

petermichaux

12/9/2005 5:48:00 AM

Thanks for all the responses.

Could someone describe how symbols are stored in memory?

Something like (I don't know if any of these are correct)

int 32 bytes
float 64 bytes
string 8 bytes for the pointer and a byte for each character including
one for "\0"

Maybe something like this would make it more concrete in my brain.

Thanks,
Peter

3 Answers

Wayne Vucenic

12/9/2005 6:46:00 AM

0

Hi Peter,

On 12/8/05, petermichaux@yahoo.com <petermichaux@yahoo.com> wrote:
> Could someone describe how symbols are stored in memory?

Symbols are immediate values, so they are stored in a 32 bit integer.
(Or a 64 bit integer on a 64 bit system).

Wayne

---
Wayne Vucenic
No Bugs Software
"Ruby and C++ Agile Contract Programming in Silicon Valley"


daz

12/9/2005 7:07:00 AM

0


peter michaux wrote:
>
> Could someone describe how symbols are stored in memory?
>
> Something like (I don't know if any of these are correct)
>
> int 32 bytes
> float 64 bytes
> string 8 bytes for the pointer and a byte for each character including
> one for "\0"
>

An int is the key into a table which holds the name.
Script reveals allocation as "previous + 1" sequence.


puts ' :name object_id >>11 (i>>3)'
puts '-'*30
s = 's_00'
5.times do |n|
sym = s.next!.to_sym
syid = sym.object_id
raise if syid & 0x7ff != 0x10e
puts ' :%s %08x %d (%d)' % [sym, syid, syid >> 11, sym.to_i >> 3]
end

=begin
:name object_id >>11 (i>>3)
------------------------------
:s_01 0026710e 1230 (1230)
:s_02 0026790e 1231 (1231)
:s_03 0026810e 1232 (1232)
:s_04 0026890e 1233 (1233)
:s_05 0026910e 1234 (1234)
=end


daz



MenTaLguY

12/9/2005 7:25:00 AM

0

On Fri, 2005-12-09 at 14:52 +0900, petermichaux@yahoo.com wrote:
> Thanks for all the responses.
>
> Could someone describe how symbols are stored in memory?

Integers.

They're indices into an array of unique string values, which is
accompanied by a hash table of string values to indices.

When a string is converted to a symbol, the string is looked up in the
hash table. If no entry is found, the string value is appended to the
array, and its index is recorded to the hash table. If an entry already
exists, that index (the symbol) is simply returned.

When a symbol is converted to a string, its index is looked up in the
array and that string value returned.

The values of two symbols can also be compared directly to determine
whether their associated strings are equal. This is much faster than
comparing two strings.

If you are storing the same string value multiple places, it may also
save memory to represent it with a symbol. Be careful, however. That
array never shrinks, so every unique string value you turn into a symbol
stays around until your program exits.

It's best to reserve symbols for situations where you expect a bounded
number of unique string values.

-mental