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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
94e31fd5108ef7932267dfc94412b668?d=identicon&s=25 petermichaux (Guest)
on 2005-12-05 07:10
(Received via mailing list)
Hi,

I'm still trying to grasp exactly what symbols are. I am getting a
feeling that they are related to my old friends the C pointers. Am I
getting closer?

Thanks,
Peter
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-12-05 07:18
(Received via mailing list)
On 12/5/05, petermichaux@yahoo.com <petermichaux@yahoo.com> wrote:
> I'm still trying to grasp exactly what symbols are. I am getting a
> feeling that they are related to my old friends the C pointers. Am I
> getting closer?

No. Symbols are just names--they can be considered immutable immediate
strings, but they are best considered names. There is *no* C
equivalent to a Symbol, as such. They can be used by certain methods
in various ways. The most common use, aside from keys on a hash, is as
the argument or arguments to a method that affects the definition of a
class or object. What is special here is *not* the Symbol, but the
method.

-austin
2d3ec3a83b4f8784d6853564fa0d2e77?d=identicon&s=25 dido.sevilla (Guest)
on 2005-12-05 07:22
(Received via mailing list)
On 12/5/05, petermichaux@yahoo.com <petermichaux@yahoo.com> wrote:
> I'm still trying to grasp exactly what symbols are. I am getting a
> feeling that they are related to my old friends the C pointers. Am I
> getting closer?

Not at all. The truth can hardly be further from your understanding.
Symbols are nothing but static strings. They have the advantage that
unlike normal strings, for any given interned symbol you have only one
particular instance stored in the interpreter, so provided you don't
have too many of them, they can save you a ton of memory. They also
look better and are a lot easier to type than actual Strings, and
they're generally appropriate for use where you'd usually make use of
a defined constant.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 dave (Guest)
on 2005-12-05 07:34
(Received via mailing list)
Peter asked:
> I'm still trying to grasp exactly what symbols are. I am getting a
> feeling that they are related to my old friends the C pointers. Am I
> getting closer?

I think the closest link between C pointers and symbols is that C
strings
are C pointers, and symbols are (immutable) strings.

A symbol is just an immutable string. It's used in a few ways that make
it
faster to use a symbol, for instance, to look up methods.

Kevin Clark wrote a short article entitled Understanding Ruby Symbols:
http://glu.ttono.us/articles/2005/08/19/understand...

Cheers,
Dave
4feed660d3728526797edeb4f0467384?d=identicon&s=25 billk (Guest)
on 2005-12-05 07:55
(Received via mailing list)
From: "Dido Sevilla" <dido.sevilla@gmail.com>
> >
> On 12/5/05, petermichaux@yahoo.com <petermichaux@yahoo.com> wrote:
> > I'm still trying to grasp exactly what symbols are. I am getting a
> > feeling that they are related to my old friends the C pointers. Am I
> > getting closer?
>
> Not at all. The truth can hardly be further from your understanding.

Except that, when testing for equality, comparing two symbols
essentially reduces to a pointer comparison.

So in that sense, symbols are indeed like pointers to strings, with
the added stipulation that lexically identical strings are KNOWN to
reside at the same memory address.

So, pseudocode:

  const char *a = SYMBOL("foo");
  const char *b = SYMBOL("foo");
  assert( a == b );  // symbols "foo" and "foo" KNOWN to be at same
address


Hope this helps rather than further confuses... :)

Regards,

Bill
94e31fd5108ef7932267dfc94412b668?d=identicon&s=25 petermichaux (Guest)
on 2005-12-09 06:55
(Received via mailing list)
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
D83785463666ae9bb98a0753eebc8950?d=identicon&s=25 nightphotos (Guest)
on 2005-12-09 07:48
(Received via mailing list)
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"
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-12-09 08:09
(Received via mailing list)
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
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-12-09 08:25
(Received via mailing list)
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
This topic is locked and can not be replied to.