[plt-scheme] Variable-length C structures in MzScheme

From: Daniel Silva (daniel.silva at gmail.com)
Date: Wed Aug 11 03:06:37 EDT 2004

Suppose I'm implementing my own Scheme Array type (that can be resized):

struct MySchemeArrayType {
  Scheme_Type tag; // my_scheme_array_type;
  int len;
  Scheme_Object* items[];

my_scheme_array_type = scheme_make_type("my-array");

You would create a new empty array with new_array:

MySchemeArrayType* new_array() {
  MySchemeArrayType* arr = (MySchemeArrayType*)
  arr->tag = my_scheme_array_type;
  arr->len = 0;
  return arr;

You would insert objects with array_insert:

Scheme_Object* array_insert(int argc, Scheme_Object* argv[])
  MySchemeArrayType* arr = (MySchemeArrayType*)
  arr->items = scheme_realloc(arr->items,
                              (arr->len + 1) * sizeof(Scheme_Object*));
  arr->items[arr->len] = argv[0];
  return scheme_void;

The scheme_realloc function uses memcpy to fill in old values in the
new, larger items C array.

I have a situation like this and after some time (and I guess a few GC
runs), the pointers in items[] point to invalid memory addresses.  Is
it that the GC thinks the array object is smaller than it really is,
and is then not seeing the pointers in items[]?


Posted on the users mailing list.