It's unlikely that it is actually crashing inside GC_MALLOC (unless things
have already gone badly wrong; assuming sizeof(object) is fairly sensible,
eg. 24 bytes).
That's exactly what it was.
Possibly you no longer need refcount if using GC. I would guess any code
that does things with refcount would need to be suppressed in case it
interferes. What have you done with the free() calls?
Actually refcount is never used. As for the free() calls, there are
none. This is my first project with C and only the 3rd interpreter
I've ever written so I wanted to keep it simple and did no memory
management.
These pointers of course will all start with garbage values, but that's also
the case under malloc() (unless you're relying perhaps on chance zero fields
from malloc that is no longer the case with GC_MALLOC).
I'm not sure exactly what you mean here. It was my understanding that
union meant "only one of these". Here's an example of the fixnum
constructor that is used in the code...
object *make_fixnum(long value) {
object *obj;
obj = alloc_object();
obj->type = FIXNUM;
obj->data.fixnum = value;
return obj;
}
So I don't think I'm relying on any zero fields. Though, honestly, I
don't have enough experience with C to know.
Is this actual code that is failing? I would check that GC_MALLOC() has
actually been properly linked in then, and the correct prototype has been
compiled. But this can be tested without involving 'object': eg.
GC_MALLOC(24).
From my run of the debugger I believe that it is the code that is
failing. I have successfully run the following test code...
#include <stdio.h>
#include <stdlib.h>
#include <gc/gc.h>
int main() {
char *a = GC_MALLOC(sizeof(char)*3);
a[0] = 'h';
a[1] = 'i';
a[2] = '\0';
printf("%s\n", a);
return 0;
}
Compiled with: gcc -lgc -o test test.c
This code works just fine and I have both #include <gc/gc.h> in the
source and -lgc in the gcc command. I have also tried to put...
void * GC_malloc(size_t size);
void GC_free(void *ptr);
void * GC_realloc(void *ptr, size_t size);
in the source and...
+ cc ... -I/usr/include/gc -lgc
in the gcc command.
My code is heavily based on Bootstrap Scheme by Peter Michaux. I
tried to use GC_MALLOC with his v0.1 code (which is relatively
simplistic) and it also produced a segfault. Code for that can be
found below.
http://github.com/petermichaux/bootstrap-scheme/tree/v0.1
I made sure to add the appropriate #include and -lgc options.
Thanks
Jack Trades