# Memory alignment

**URL:** <https://www.ruby-forum.com/t/memory-alignment/92649>\
**Category:** Ruby\
**Created:** [April 22, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/memory-alignment/92649 "2007-04-22T15:09:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [April 22, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/memory-alignment/92649/1 "2007-04-22T15:09:00Z")

</div>

Hi,

in an C extension i need to generate ruby strings, where str-\>ptr is  
aligned to a 16 byte boundary. This is necessary so that the data can be  
used with simd engines (AltiVec, SSE, …).

Is there a way to do this?

Cheers  
detlef

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [April 22, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/memory-alignment/92649/2 "2007-04-22T15:51:05Z")

</div>

Detlef R. wrote:

> 

There’s no way to tell Ruby to align its strings this way. Your only  
option is to make a copy aligned to your needs.

---

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [April 24, 2007, 2:42am UTC](https://www.ruby-forum.com/t/memory-alignment/92649/3 "2007-04-24T02:42:15Z")

</div>

On 4/22/07, Timothy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > detlef
> 
> There’s no way to tell Ruby to align its strings this way. Your only option is to make a copy aligned to your needs.

Without looking at the ruby source code to see if there is a hack for  
alignment, one thought that occurs is to create your string 16 bytes  
larger than required – find the correct offset that is aligned, do  
the work and then adjust the string as necessary to the correct start.

pth
