# c extension causing segmentation fault

**URL:** <https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [December 29, 2013, 5:24pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559 "2013-12-29T17:24:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adnan\_gazi01](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@adnan\_gazi01](https://forum.kx.com/u/adnan_gazi01)\
**Post date:** [December 29, 2013, 5:24pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559/1 "2013-12-29T17:24:00Z")

</div>

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)  
{&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; int p = port-\>i;  
&nbsp;&nbsp;&nbsp; char \*d = dict-\>s;  
&nbsp;&nbsp;&nbsp; printf(“Port: %d, Dict: %s”,p,d);  
&nbsp;&nbsp;&nbsp; char \*command = (“java -jar $QHOME/l32/gui.jar %d %s”);  
&nbsp;&nbsp;&nbsp; int outsize = sizeof(command) + sizeof(d) + 4;  
&nbsp;&nbsp;&nbsp; char \*out = malloc(outsize);  
&nbsp;&nbsp;&nbsp; sprintf(out, command, p, d);  
&nbsp;&nbsp;&nbsp; &nbsp;  
&nbsp;&nbsp;&nbsp; system(out);  
&nbsp;&nbsp;&nbsp; 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](mailto: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

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [December 29, 2013, 6:06pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559/2 "2013-12-29T18:06:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adnan\_gazi01](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@adnan\_gazi01](https://forum.kx.com/u/adnan_gazi01)\
**Post date:** [December 29, 2013, 6:29pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559/3 "2013-12-29T18:29:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [December 29, 2013, 6:32pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559/4 "2013-12-29T18:32:00Z")

</div>

q)string 1234

“1234”

q)string `hello`world

“hello”

“world”

---

<div class="post-metadata">

**Author:** ![adnan\_gazi01](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@adnan\_gazi01](https://forum.kx.com/u/adnan_gazi01)\
**Post date:** [December 29, 2013, 6:39pm UTC](https://forum.kx.com/t/c-extension-causing-segmentation-fault/8559/5 "2013-12-29T18:39:00Z")

</div>

Wow. Thanks a lot! this has really helped.
