This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

segfaults and Re: structs, records, SMOBs etc.


On Wed, Jul 22, 1998 at 10:45:40PM -0400, Jim Blandy wrote:
> 
> > [1] I'm writing a lot of scm->C and C->scm translations into everything,
> >     they don't seem to make much difference to the speed (though they
> >     can't be helping) but they do obfuscate the code and forgetting to
> >     include a translation (especially C->scm) is usually instant segmentation
> >     violation signals.
> 
> Why aren't you getting typecheck errors?  The scheme values should be
> of type "SCM", and the C values should be pointers or something.
> 
> That would be quicker than waiting for segfaults.

I mean that you have a C routine that generates a small integer,
like 0 - 100 for example, and you want to return the integer so 
you type:

return( x );

where you SHOULD have used:

return( gh_int2scm( x ));

So the compiler says, ``no problem'', because SCM is essentially
an integer anyhow and it will sliently typecast to most things (except
pointers) without a problem, even if you return a pointer by accident,
you only get a warning, no actual typecheck error. Once you get
smallish integer values running around in guile it's game over.

When you have various versions of the same function that take
and some return C data and some return SCM data, it requires a bit
of concentration.

In some ways it would be nice to make SCM actually a struct or a union
containing an integer, our current declaration is:

/* In the beginning was the Word:
 */
typedef long SCM;


maybe something like this would be better: 

typedef union
{
	long l;
	void *v;
}	SCM;

Using the above, you can still get away with:

SCM x = 0;

but just about anything else will pull up a typecheck error real quick.
Mind you, changing the SCM typedef would require changing just about
every macro in tags.h but only in the way of adding either .l or .v
after various things. In gcc, unions can do smart things with typecasts
but this is probably not good to depend on.

In C++ you would use a class for this purpose and all the type conversion
functions could be automatic (one of the nice things about C++ to be sure)
Well maybe there will eventually be a C++ version of guile, at very
least a C++ class for the SCM type would make a nice header file.

Guess I'll stick to C in the short term though.

	- Tel