I have trouble even getting adding value n returned when I tried to use
Fiddle::Function.new, I returned to my old way of using Fiddle:
class SamplesizeController < ApplicationController
require 'fiddle'
require 'fiddle/import'
module Libm
extend Fiddle::Importer
dlload '/var/www/myapp/smart/lib/lib_add.so'
extern 'long add(long, double, double, char *)'
end
def compute
session[:n] = Libm.add( session[:nmax], session[:delta],
session[:conf], session[:errmsg] )
end
...
end
This way, my session[:n] is returned correctly, even I couldn't
get session[:errmsg].
Before, I was able to pass session[:errmsg] and get out
errormsg (don't need to deal with Fiddle::Pointer), my
c code, I didn't use malloc for errmsg, but copy another
char * to errmsg using a for loop...
Liz
On Monday, November 23, 2015 at 1:02:36 PM UTC-5, Liz Huang wrote:
>
>
>
> If I don't allocate a new buffer, then I don't need pointer to a pointer?
> For example, I
> just copy an existing char * iarr to errMsg?
>
>
> long add(long maxn, double delta, double conf, char *errMsg)
> {
> long answer;
> int i;
> char *iarr;
>
> //errMsg = (char *) malloc(6*sizeof(char));
> answer = (long)(maxn + delta + conf);
> iarr = "Hello!";
> for(i=0;i<6;i++) errMsg[i] = (char) iarr[i];
>
> return answer;
> }
>
> The document for Fiddle is so limited, wonder where I could find an
> example doing
> what you said? Now I can just guess and try, guess I use
>
> buf = Fiddle::Pointer.malloc(8)
>
> then pass buf to C function,
>
> then try to convert string from buf returned?
>
>
>
> Liz
>
>
> On Monday, November 23, 2015 at 12:21:27 PM UTC-5, Frederick Cheung wrote:
>>
>> On Monday, November 23, 2015 at 3:25:07 PM UTC, Liz Huang wrote:
>>>
>>>
>>>
>>> Thanks!
>>>
>>> Strange that I am able to get error message before just using char *,
>>> thought
>>> it meant passing string by pointer already.
>>>
>>> it does, but if you want to allocate a new buffer and have the caller
>> access that then you need pointer to a pointer
>>
>>
>>> I change the argument to char ** errMsg, but in Ruby, when I tried to
>>> convert the pointer to string, I got error message:
>>>
>>> [snip]
>>
>>
>>> if options.is_a?(::JSON::State)
>>> # Called from JSON.{generate,dump}, forward it to JSON gem's to_json
>>> self.to_json_without_active_support_encoder(options)
>>> else
>>> # to_json is being invoked directly, use ActiveSupport's encoder
>>> ActiveSupport::JSON.encode(self, options)
>>>
>>> Do I need to change anything in ruby file when use a pointer?
>>>
>>>
>> Yes. You need to allocate a pointer size bit of memory for the C function
>> (via Fiddle::Pointer). The C function fills that in, you then use the ptr
>> method on Fiddle::Pointer to get the memory allocated by the C function,
>> and create your string from there. (don't forget to free the memory too)
>>
>> Fred
>>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/cc868088-c874-469b-a2ef-3a1f8c2db084%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.