Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Iterating a linked list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Navaneeth KN  
View profile  
 More options May 7 2012, 1:55 pm
From: Navaneeth KN <navaneet...@gmail.com>
Date: Mon, 7 May 2012 10:55:35 -0700 (PDT)
Local: Mon, May 7 2012 1:55 pm
Subject: Iterating a linked list

Hello,

I recently started using FFI and I have to say it is one of the best
libraries I have ever used. Very neat syntax and performance. Thanks for
such a great library.

Everything was working fine for me untill I got to iterate over a linked
list. I am getting a segmentation fault when used from FFI. My C code works
well and all tests are passing. My C function is like,

extern int
get_all_tokens(
    object *handle,
    int token_type,
    struct token **tokens
);

This function points the head of the linked list to "tokens" variable. Here
is my FFI code.

  token_ptr = FFI::MemoryPointer.new :pointer
  done = Library.get_all_tokens($handle.get_pointer(0), 1, token_ptr);
  if done != 0
    error_message = Library.get_last_error($handle.get_pointer(0))
    error error_message
    return
  end

  ptr = token_ptr.read_pointer

  until ptr.null?
    item = Library::Token.new(ptr)
    puts item[:pattern] ----> Fails here
    ptr = item[:next]
  end

My C structure looks like,

struct token {
    int type, match_type;
    char tag[TOKEN_TAG_MAX];
    char pattern[SYMBOL_MAX];
    char value1[SYMBOL_MAX];
    char value2[SYMBOL_MAX];
    int children;
    struct token* next;

};

And here is the FFI equivalent one,

  class Token < FFI::Struct
    layout :type, :int,
           :match_type, :int,
           :tag, :string,
           :pattern, :string,
           :value1, :string,
           :value2, :string,
           :children, :int,
           :next,  :pointer
  end

When running this program, I am getting

[BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

Any clue to resolve this issue would be helpful.

Thanks
Navaneeth


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wayne Meissner  
View profile  
 More options May 7 2012, 6:26 pm
From: Wayne Meissner <wmeiss...@gmail.com>
Date: Mon, 7 May 2012 15:26:20 -0700 (PDT)
Local: Mon, May 7 2012 6:26 pm
Subject: Re: Iterating a linked list

You've got it almost right - the problem is your mapping of the struct
token string fields.

e.g.
  char tag[TOKEN_TAG_MAX];

instead of
  :tag, :string

it should be:

  :tag, [ :char, TOKEN_TAG_MAX ]

Similar for pattern, value1, value2.  Of course, you'll also need to define
TOKEN_TAG_MAX and SYMBOL_MAX.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Navaneeth KN  
View profile  
 More options May 8 2012, 12:48 am
From: Navaneeth KN <navaneet...@gmail.com>
Date: Mon, 7 May 2012 21:48:23 -0700 (PDT)
Local: Tues, May 8 2012 12:48 am
Subject: Re: Iterating a linked list

Hello,

Great. Works now. Thanks a lot for the help.

Navaneeth


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »