- Subject: Problems with structures in S-Lang
- From: Goran Koruga <goran.koruga@xxxxxxxxx>
- Date: Mon, 7 Feb 2000 08:23:29 +0100
Hi,
I encountered some problems using structures in S-Lang. I would be glad
if anyone could please explain the difference between the working and non-
working example for me.
Here are the relevant pieces of the code I'm using :
------ non working example
#include <sys/time.h>
#include <unistd.h>
/* man page for gettimeofday() says (the acutal definition uses int
instead of long, but int = long on Linux)
struct timeval {
long tv_sec;
long tv_usec;
};
*/
static struct timeval *Msl_timestamp; /* it's malloc'd in main() */
/* here's the definition of the structure for S-Lang */
static SLang_IStruct_Field_Type timeval_struct[] =
{
MAKE_ISTRUCT_FIELD(struct timeval, sec, "sec", SLANG_UINT_TYPE, 1),
MAKE_ISTRUCT_FIELD(struct timeval, usec, "usec", SLANG_UINT_TYPE, 1),
SLANG_END_TABLE
};
/* we add the structure this way */
SLadd_istruct_table(timeval_struct, (VOID_STAR) &Msl_timestamp, "timestamp");
/* here's how I set it */
void Msl_set_timestamp(void)
{
gettimeofday(Msl_timestamp, NULL);
}
/* my program crashes whenever I access members of the timestamp variable
in S-Lang */
sprintf("%d %d", timestamp.sec, timestamp.usec);
Note that I have tried to use various S-Lang defined types to access the
members of the timestamp structure, but the result was always the same.
------ end of non working example
Here's the working example :
------ working example
/* I added a new C structure for this case */
typedef struct timeval_struct
{
unsigned int sec;
unsigned int usec;
} timeval_elt;
timeval_elt *Msl_timestamp = NULL; /* it's malloc'd in main() */
/* here's the definition of the structure for S-Lang */
static SLang_IStruct_Field_Type timeval_struct[] =
{
MAKE_ISTRUCT_FIELD(timeval_elt, sec, "sec", SLANG_UINT_TYPE, 1),
MAKE_ISTRUCT_FIELD(timeval_elt, usec, "usec", SLANG_UINT_TYPE, 1),
SLANG_END_TABLE
};
/* we add the structure this way */
SLadd_istruct_table(timeval_struct, (VOID_STAR) &Msl_timestamp, "timestamp");
/* here's how I set it */
void Msl_set_timestamp(void)
{
static struct timeval timestamp;
gettimeofday(×tamp, NULL);
Msl_timestamp->sec = timestamp.tv_sec;
Msl_timestamp->usec = timestamp.tv_usec;
}
/* my program works whenever I access members of the timestamp variable
in S-Lang */
sprintf("%d %d", timestamp.sec, timestamp.usec);
------ end of working example
Thanks in advance,
Goran
--
Writing about music is like dancing about architecture.
-- Frank Zappa
[2000 date index]
[2000 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]