Skip to content

Instantly share code, notes, and snippets.

@yashi
Created November 29, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashi/46dae815b3c5a4751204 to your computer and use it in GitHub Desktop.
Save yashi/46dae815b3c5a4751204 to your computer and use it in GitHub Desktop.
/* -*- compile-command: "gcc -Wall -Wextra -g $(pkg-config --cflags --libs gio-2.0) transfer-full-in.c" -*- */
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
void *buf;
gsize len;
GBytes *bytes;
len = 100;
buf = malloc(len);
sprintf(buf, "%s", "hello");
bytes = g_bytes_new_take(buf, len);
// It might be a good idea to remove the reference to the buf.
// This ensures that we are no longer able to free it by
// accident.
buf = NULL;
len = 0;
fwrite(g_bytes_get_data(bytes, NULL),
g_bytes_get_size(bytes),1, stdout);
// buf will be freed when byts are freed
g_bytes_unref(bytes);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment