Help in Pointer

Hello

I am a beginner to Ruby. I want to know how to hide a value with pointer
which is not in the stack.

Regards

Tridib

On Thu, Jul 28, 2011 at 7:47 PM, Tridib B.
[email protected]wrote:

Posted via http://www.ruby-forum.com/.

I don’t know what this means, can you be more explicit or give an
example?
Are you talking about the stack as in the place you store memory that
isn’t
the heap? What does “hide a value” mean?

Is this what your looking for:
http://macdevelopertips.com/ruby/using-a-stack-with-ruby.html

~Stu

Tridib B. [email protected] wrote:

Actually i am trying to hide a data behind the pointer which is not in
the stack which the Garbage Collector keeps track of.

rb_global_variable(). It’s documented in README.EXT of the Ruby source.

Tridib -

This is an interesting question.

Can you say more about why you want to do this?

  • Keith

Tridib what did you try?
are you working in ruby only?
ruby itself does not have pointer

or make you an c lib for ruby?

then if you wrap the struct does not set an mark or free func

Stu wrote in post #1013651:

Is this what your looking for:
http://macdevelopertips.com/ruby/using-a-stack-with-ruby.html

~Stu

No.

Actually i am trying to hide a data behind the pointer which is not in
the stack which the Garbage Collector keeps track of.

Regards

Tridib

Hello

I got to know from this forum before that the Garbage Collector just
scans the stack looking for pointers. So, if we allocate a pointer and
hide data behind the pointer the Garbage Collector won’t see pointers
which aren’t on the stack.

As I am working on Garbage Collector and want to implement manual memory
deallocation so I need to hide data from the garbage collector applying
the above technique.

Tridib

On Aug 1, 2011, at 6:11 PM, Tridib B. wrote:

I got to know from this forum before that the Garbage Collector just
scans the stack looking for pointers. So, if we allocate a pointer and
hide data behind the pointer the Garbage Collector won’t see pointers
which aren’t on the stack.

As I am working on Garbage Collector and want to implement manual memory
deallocation so I need to hide data from the garbage collector applying
the above technique.

You can inform the garbage collector of your pointer through
Data_Wrap_Struct and Data_Make_Struct. From README.EXT:

function to free the pointer allocation. If this is -1, the pointer
will be just freed. The functions mark and free will be called from
garbage collector.

Does this answer your questions?

For further reading:

http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/README.EXT?view=markup

You can inform the garbage collector of your pointer through
Data_Wrap_Struct and Data_Make_Struct. From README.EXT:

function to free the pointer allocation. If this is -1, the pointer
will be just freed. The functions mark and free will be called from
garbage collector.

Does this answer your questions?

For further reading:

http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/README.EXT?view=markup

So does it delete only the pointer or the pointer with the data its
pointing?

Its not clear to me.

Actually I want to perform memory leak. Which the garbage collector will
not be notified of but I will be of the memory leak and its source of
leak.

Tridib

On Wed, Aug 3, 2011 at 9:45 AM, Hans M. [email protected] wrote:

Data_Wrap_Struct(klass, 0, 0, ptr)
the pointer will not be deleted or freed

the problem is: sometimes its bad if the GC deleted objects if there are
stored in other c(++) objects (like an window manager which holds the
windows) this way you need Data_Wrap_Struct(klass, 0, 0, ptr)

PS: i does not realy know that mark do, so i does not use it

If I understand correctly, you have to use the mark function if you
are referencing other Ruby objects from your object, so that the GC
knows those objects are alive, since they are referenced from yours.

Jesus.

Data_Wrap_Struct(klass, mark, free, ptr)

if you use
Data_Wrap_Struct(klass, 0, free, ptr)
or
Data_Wrap_Struct(klass, 0, -1, ptr)

the pointer and the data will be freed
if you use
Data_Wrap_Struct(klass, 0, 0, ptr)
the pointer will not be deleted or freed

the problem is: sometimes its bad if the GC deleted objects if there are
stored in other c(++) objects (like an window manager which holds the
windows) this way you need Data_Wrap_Struct(klass, 0, 0, ptr)

PS: i does not realy know that mark do, so i does not use it

On Aug 6, 2011, at 10:08 PM, Tridib B. wrote:

if you use
Data_Wrap_Struct(klass, 0, 0, ptr)
the pointer will not be deleted or freed

So will it make Memory Leak…!!!

No. If your object can be freed with free() then everything is fine.
If your object needs to do special things then implement a custom free
function.

Ruby will collect the ruby objects inside your pointer when the GC knows
it is OK provided you implement a mark function. You should have
something like this:

static void
my_mark(my_pointer *ptr) {
rb_gc_mark(ptr->ruby_object);
}

Data_Wrap_Struct(klass, my_mark, -1, ptr);

Eric H. wrote in post #1015428:

On Aug 6, 2011, at 10:08 PM, Tridib B. wrote:

if you use
Data_Wrap_Struct(klass, 0, 0, ptr)
the pointer will not be deleted or freed

So will it make Memory Leak…!!!

No. If your object can be freed with free() then everything is fine.
If your object needs to do special things then implement a custom free
function.

Yeah thats what i am trying to do implement my custom free function.

Ruby will collect the ruby objects inside your pointer when the GC knows
it is OK provided you implement a mark function. You should have
something like this:

static void
my_mark(my_pointer *ptr) {
rb_gc_mark(ptr->ruby_object);
}

Data_Wrap_Struct(klass, my_mark, -1, ptr);

Ok even if i make my own mark function will the GC keep track of the
pointer and the object behind it.

Secondly, the object which is been marked by my custom function will it
be deleted by GC or by my own custom free function.

Is there anyone who can answer to my previous post???

Regards

Tridib

Hans M. wrote in post #1014677:

Data_Wrap_Struct(klass, mark, free, ptr)

if you use
Data_Wrap_Struct(klass, 0, free, ptr)
or
Data_Wrap_Struct(klass, 0, -1, ptr)

the pointer and the data will be freed
if you use
Data_Wrap_Struct(klass, 0, 0, ptr)
the pointer will not be deleted or freed

So will it make Memory Leak…!!!

the problem is: sometimes its bad if the GC deleted objects if there are
stored in other c(++) objects (like an window manager which holds the
windows) this way you need Data_Wrap_Struct(klass, 0, 0, ptr)

PS: i does not realy know that mark do, so i does not use it

I need this to happen… So that i can implement… Manual memory
deallocation…!!!

ANd secondly… where should i Put this code in… on which function??

Tridib

Thanks

Where should I debug that code fragment(mymark)?

Inside gc.c under which function? or should it be separate code?

Regards

Tridib

the mark function is not important for your problem

will it be deleted by GC or by my own custom free function.

it will be deleted by the GC with your own free function you give them
to the GC (the third parameter)