c extension causing segmentation fault

I’ve written a shared object in C containing a function that takes a K int and K symbol as args, and starts a java program using those variables as its parameters by calling system(). It’s giving me a segmentation fault when I call the q function that calls the shared object, and q decides to kill it after telling me it encountered a fatal error. Ive stripped the C code down but it seems to be the case that the error stems from using the 2 K parameters in the SO. The C code and q error log are pasted below. I’m running the 32 bit trial q and have compiled the shared object as 32 bit.

C code:

#define KXVER 3
#include “k.h”
#include <stdlib.h>
#include <stdio.h>

K start(K port, K dict)
{   
       
    int p = port->i;
    char *d = dict->s;
    printf(“Port: %d, Dict: %s”,p,d);
    char *command = (“java -jar $QHOME/l32/gui.jar %d %s”);
    int outsize = sizeof(command) + sizeof(d) + 4;
    char *out = malloc(outsize);
    sprintf(out, command, p, d);
     
    system(out);
    return 0;
}

And q’s error is this:

Sorry, this application or an associated library has encountered a fatal error and will exit.
If known, please email the steps to reproduce this error to tech@kx.com
with a copy of the kdb+ startup banner.
Thank you.
q() [0x80af848]
[0xf7726410]
/lib32/libc.so.6(_IO_vfprintf+0x4519) [0xf7527b79]
/lib32/libc.so.6(_IO_printf+0x2f) [0xf752d44f]
/home/adnan/q/l32/kge.so(start+0x40) [0xf772262b]
q(dlc+0xbf) [0x80ae92f]
q() [0x8087e2c]
q() [0x8088ccb]
q() [0x8087b8c]
q(tix+0x15d) [0x808992d]
q(kx+0x1f) [0x808a0ff]
q(f1+0x1a) [0x80669aa]
q(c9+0x2f8) [0x80892e8]
q(f2+0x93) [0x8066aa3]
q() [0x804f1ad]
rlwrap: warning: q killed by SIGSEGV.
rlwrap has not crashed, but for transparency,
it will now kill itself (without dumping core)with the same signal

Segmentation fault

The java program is meant to start a gui displaying the variables in namspace “dict” on port “port”. I’ve been developing it for a while now, and it works fine on its own. A shared object with no parameters calls the java program if I set the port and namespace myself, but otherwise it doesn’t seem to be working.

Any help would be greatly appreciated.

Thanks

btw, have you tried the q command system?
q)system"java …. &"

In your c code, i think you have a bug with sizeof - should use strlen.

AHHH no i hadn’t realised there was a system command. Damn.
The system command works though, which is good. Is there a way to cast an integer or symbol as a string? so I can use it in the system call? The C function would’ve taken K objects

q)string 1234

“1234”

q)string helloworld

“hello”

“world”

Wow. Thanks a lot! this has really helped.