Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:10:51+00:00 2026-06-11T00:10:51+00:00

I am considering writing a C-based Ruby gem to speed up text wrapping in

  • 0

I am considering writing a C-based Ruby gem to speed up text wrapping in Prawn. I’ve read a very small portion of the C source for MRI before, but don’t know the API for building extensions well yet.

Within my C code, I’d like to get a direct pointer to the data within a Ruby String, and walk over it byte by byte. Further to that, I’d like to store pointers within the buffer in my own struct, and use them not only within the scope of a single call, but within subsequent calls into the extension code.

Is this possible? Could the GC move the Strings, rendering my pointers invalid? And also, how can I let Ruby know that I am holding pointers to the Strings in my own structs (so the GC doesn’t try to reclaim them)? Can this code be written in a way which is compatible with both MRI 1.8 and 1.9?

And since I’m asking about using pointers safely in a C-based Ruby extension: can I use malloc and free the same as I would in a “regular” C-based project?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:10:52+00:00Added an answer on June 11, 2026 at 12:10 am

    The link that matt offers is really good. It would have saved me days if I had found it before.

    You can keep references to ruby Strings and pointers into them. I would suggest freezing the String. Then every attempt to change the string will fail. There is a function Data_Wrap_Struct() that lets you wrap your own data structure into a Ruby object. Beside the data structure and the class of the structure, the function takes two function arguments. One of them (mark) is used to show the garbage collector where your structure references other ruby objects.

    What took me some time to understand, is that the garbage collector is really scanning the stack of all ruby threads to seek for references to ruby objects. So keeping VALUEs on the stack is also a safe method to keep objects referenced.

    Can this code be written in a way which is compatible with both MRI 1.8 and 1.9?

    The basic API for extensions didn’t change very much (I think) from 1.8 to 1.9. But I’ve used only 1.9 so far.

    can I use malloc and free the same as I would in a “regular” C-based project?

    Sure, I cannot think of any reason why this should not possible, as long as you don’t expect the garbage collector to keep care of the allocated memory.

    I had a hard time, mixing C++ code, compiled with another version of gcc than the version the ruby interpreter was compiled with. If you experience strange startup behavior, I would check for compiler version differences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently considering writing a small game. It is essentially a map where
I am writing a small game in android that basically is a turn-based boardgame,
I'm considering writing a custom IPermission implementation but am not clear as to how
I'm writing a windows application that is embed with a manifest considering UAC in
We're writing a large production system in Java, and I'm considering whether or not
Considering it's a common issue, what could be the source of this error if
I'm considering writing a cross-platform desktop app, initially for Mac/Windows, but eventually for Linux
I'm considering writing my own tool for tracking visitors/sales as Google Analytics and others
I'm considering writing my own delivery code using PowerShell and/or C#, maybe shelling to
I'm considering writing a new Windows GUI app, where one of the requirements is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.