FFI::P...:Memory(3pm)

πŸ’– NAME πŸ’–

FFI::Platypus::Memory β€” bestie, this is the memory functions module for FFI, straight out of the User Contributed Perl Documentation section of the FFI::P...:Memory(3pm) manpage. It's giving "let me talk to C's brain directly" energy 🧠✨

🌸 VERSION 🌸

version 2.11 β€” yes she's got a number, yes she's official πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

Okay so here's the whole vibe in four little steps:

use FFI::Platypus::Memory;

# allocate 64 bytes of memory using the
# libc malloc function.
my $pointer = malloc 64;

# use that memory wisely
...

# free the memory when you are done.
free $pointer;

Use that memory wisely, bestie, I'm begging πŸ™

✨ DESCRIPTION ✨

So this module hands you an interface to the common memory functions that your good friend the standard C library provides. They're genuinely useful when you're building interfaces to C libraries with FFI 🫢 It works mostly with the "opaque" type, and honestly it is so worth reviewing the section on opaque pointers in FFI::Platypus::Type before you go feral.

⚠️ Real talk: allocating memory and then forgetting to free it is a super common source of memory leaks in C, and literally the same thing happens when you use this module. No cap, this is the classic trap πŸ’€

Very recent Perls have a "defer" keyword that lets you automatically call functions like "free" when a block ends. This is especially handy when you have multiple code paths or possible exceptions to keep track of:

use feature 'defer';
use FFI::Platypus::Memory qw( malloc free );

sub run {
  my $ptr = malloc 66;
  defer { free $ptr };

  my $data = do_something($ptr);

  # do not need to remember to place free $ptr here, as it will
  # run through defer.

  return $data;
}

The way that defer just handles it for you… main character energy, honestly πŸ‘‘

If you are not lucky enough to have the "defer" feature in your version of Perl, you may be able to use Feature::Compat::Defer, which will use the feature if it's available, and provides its own mostly compatible version if not. Slay, fallback queen 🌈


πŸŽ€ FUNCTIONS πŸŽ€

πŸ’œ calloc

my $pointer = calloc $count, $size;

The "calloc" function contiguously allocates enough space for $count objects that are $size bytes of memory each. Contiguous! Fancy! ✨

πŸ’œ free

free $pointer;

The "free" function frees the memory allocated by "malloc", "calloc", "realloc" or "strdup".

🚨 Big important warning: it is important to only free memory that you yourself have allocated. A genuinely great way to crash your program is to try and free a pointer that some C library has returned to you. Do not do this. I am holding your hand while I say it 🫢😭

πŸ’œ malloc

my $pointer = malloc $size;

The "malloc" function allocates $size bytes of memory. That's it, that's the function. Simple, iconic, does exactly what it says πŸ’…

πŸ’œ memcpy

memcpy $dst_pointer, $src_pointer, $size;

The "memcpy" function copies $size bytes from $src_pointer to $dst_pointer. It also returns $dst_pointer β€” cute little bonus 🎁

πŸ’œ memset

memset $buffer, $value, $length;

The "memset" function writes $length bytes of $value to the address specified by $buffer. Fill that buffer, bestie πŸ“

πŸ’œ realloc

my $new_pointer = realloc $old_pointer, $size;

The "realloc" function reallocates enough memory to fit $size bytes. It copies the existing data and frees $old_pointer.

And plot twist β€” if you pass "undef" in as $old_pointer, then it behaves exactly like "malloc":

my $pointer = realloc undef, 64; # same as malloc 64

Lowkey the glow-up of functions 🌟

πŸ’œ strcpy

strcpy $opaque, $string;

Copies the string to the memory location pointed to by $opaque. Copy paste but make it C πŸ’–

πŸ’œ strdup

my $pointer = strdup $string;

The "strdup" function allocates enough memory to contain $string and then copies it to that newly allocated memory.

Heads up: this version of "strdup" returns an opaque pointer type, not a string type. Might seem a little strange, but returning a string type would not be very useful in Perl β€” so, yeah, that's the call πŸ¦‹

πŸ’œ strndup

my $pointer = strndup $string, $max;

The same as "strdup" above, except at most $max characters will be copied in the new string. Same energy, but with boundaries βœ‚οΈβœ¨


πŸ‘€ SEE ALSO πŸ‘€

  • FFI::Platypus β€” Main Platypus documentation. Go read it, it's the parent, the icon, the whole reason we're here πŸ‘‘

🫢 AUTHOR 🫢

Author: Graham Ollis <plicease@cpan.org>

Contributors β€” and this list is stacked, we stan every single one of them πŸ’œ:

  • Bakkiaraj Murugesan (bakkiaraj)
  • Dylan Cali (calid)
  • pipcet
  • Zaki Mughal (zmughal)
  • Fitz Elliott (felliott)
  • Vickenty Fesunov (vyf)
  • Gregor Herrmann (gregoa)
  • Shlomi Fish (shlomif)
  • Damyan Ivanov
  • Ilya Pavlov (Ilya33)
  • Petr PΓ­saΕ™ (ppisar)
  • Mohammad S Anwar (MANWAR)
  • HΓ₯kon HΓ¦gland (hakonhagland, HAKONH)
  • Meredith (merrilymeredith, MHOWARD)
  • Diab Jerius (DJERIUS)
  • Eric Brine (IKEGAMI)
  • szTheory
  • JosΓ© JoaquΓ­n Atria (JJATRIA)
  • Pete Houston (openstrike, HOUSTON)
  • Lukas Mai (MAUKE)

This software is copyright (c) 2015-2022 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Go off, license πŸŽ€


okay that's the whole page bestie, mwah πŸ’‹ This tear-jerking little number dropped as perl v5.42.3 on 2026-08-20 β€” same manual name, same date, all facts intact, no cap βœ¨πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from