# insert inside a C extension function (again)

**URL:** <https://forum.kx.com/t/insert-inside-a-c-extension-function-again/5809>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [September 28, 2008, 4:33am UTC](https://forum.kx.com/t/insert-inside-a-c-extension-function-again/5809 "2008-09-28T04:33:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbtouch](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dbtouch](https://forum.kx.com/u/dbtouch)\
**Post date:** [September 28, 2008, 4:33am UTC](https://forum.kx.com/t/insert-inside-a-c-extension-function-again/5809/1 "2008-09-28T04:33:00Z")

</div>

Dear KDB Users,

Please take a look at this dll defined as:

#include “k.h”  
K startAud(K y)  
{  
int bulkSize=3;  
K x=knk(6, ktn(KI, bulkSize), ktn(KC, bulkSize), ktn(KH, bulkSize),  
ktn(KI, bulkSize), ktn(KF, bulkSize), ktn(KS, bulkSize));  
for(int i=0; ikI(xK[0])[i] = i;  
kC(xK[1])[i] = ‘a’;  
kH(xK[2])[i] = 100;  
kI(xK[3])[i] = 1000;  
kF(xK[4])[i] = 99.9;  
kS(xK[5])[i] = (S)“hello world”;  
}  
err=k(0, “insert”, ks((S)“all\_dt”), x, (K)0);  
k(0,“”,(K)0);  
return ki(y-\>i);  
}  
  
When I tested it: the insert somehow did not happen. Could you tell me  
what I missed? Do I have to pass X in as a parameter? Or I cannot  
allocate x using knk inside the function?  
  
To run the test:  
  
1. compile the code as DLL  
2. create table as:  
all\_dt:(seq:`int$();cchar:`char$();cshort:`short$();cint:`int$  
();cfloat:`float$();csym:`symbol$();time:`time$())  
3. load the dll and run the test  
4. show all\_dt in q console.

---

<div class="post-metadata">

**Author:** ![Lindqvist](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@Lindqvist](https://forum.kx.com/u/Lindqvist)\
**Post date:** [September 28, 2008, 7:05pm UTC](https://forum.kx.com/t/insert-inside-a-c-extension-function-again/5809/2 "2008-09-28T19:05:00Z")

</div>

I altered the code slightly to make it work, the basic problem wasthat the time column had been left out on the C side (the call to knkand - obviously - in the for loop):#include \<stdio.h\>#include "k.h"K startAud(K y){ int bulkSize=3; int i; K err = NULL; K x=knk(7, // Set to 7 as we are adding time ktn(KI, bulkSize), ktn(KC, bulkSize), ktn(KH, bulkSize), ktn(KI, bulkSize), ktn(KF, bulkSize), ktn(KS, bulkSize), ktn(KT, bulkSize)); // Add time for(i=0; ii; // Time is simple scalar } err = k(0, “insert”, ks((S)“all\_dt”), x, (K)0); if (err-\>t == -128) O(“err = %s \n”, err-\>s); return ki(y-\>i);}Creating a nice small test script:cat \> t.qall\_dt:( seq:`int$(); cchar:`char$(); cshort:`short$(); cint:`int$(); cfloat:`float$(); csym:`symbol$(); time:`time$())startAud:`t 2:(`startAud; 1)startAudshow all\_dt^DLoading the script for a go with the code:q) \l t.q0seq cchar cshort cint cfloat csym time-----------------------------------------------------0 a 100 1000 99.9 hello world 00:00:00.2001 a 100 1000 99.9 hello world 00:00:00.2002 a 100 1000 99.9 hello world 00:00:00.200…seems to have done the trick.H?kan\</stdio.h\>

---

<div class="post-metadata">

**Author:** ![dbtouch](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@dbtouch](https://forum.kx.com/u/dbtouch)\
**Post date:** [September 29, 2008, 2:58am UTC](https://forum.kx.com/t/insert-inside-a-c-extension-function-again/5809/3 "2008-09-29T02:58:00Z")

</div>

Oops… Thanks, H?kan.dbtouchOn Sep 28, 2:05?pm, Lindqvist \<lindqvist.h…\> wrote:\> I altered the code slightly to make it work, the basic problem was\> that the time column had been left out on the C side (the call to knk\> and - obviously - in the for loop):\>\> #include \<stdio.h\>\>\> #include “k.h”\>\> K startAud(K y)\> {\> ? ? int bulkSize=3;\> ? ? int i;\> ? ? K err = NULL;\>\> ? ? K x=knk(7, ? ? ? ? ? ? ? ? ?// Set to 7 as we are adding time\> ? ? ? ? ? ? ktn(KI, bulkSize),\> ? ? ? ? ? ? ktn(KC, bulkSize),\> ? ? ? ? ? ? ktn(KH, bulkSize),\> ? ? ? ? ? ? ktn(KI, bulkSize),\> ? ? ? ? ? ? ktn(KF, bulkSize),\> ? ? ? ? ? ? ktn(KS, bulkSize),\> ? ? ? ? ? ? ktn(KT, bulkSize)); // Add time\>\> ? ? for(i=0; i ? ? ? ? kI(xK[0])[i] = i;\> ? ? ? ? kC(xK[1])[i] = ‘a’;\> ? ? ? ? kH(xK[2])[i] = 100;\> ? ? ? ? kI(xK[3])[i] = 1000;\> ? ? ? ? kF(xK[4])[i] = 99.9;\> ? ? ? ? kS(xK[5])[i] = (S)“hello world”;\> ? ? ? ? kI(xK[6])[i] = kt(200)-\>i; // Time is simple scalar\> ? ? }\>\> ? ? ?err = k(0, “insert”, ks((S)“all\_dt”), x, (K)0);\>\> ? ? if (err-\>t == -128)\> ? ? ? ? O(“err = %s \n”, err-\>s);\>\> ? ? return ki(y-\>i);\>\> }\>\> Creating a nice small test script:\>\> cat \> t.q\> all\_dt:(\> ? ? seq:`int$();&gt; ? ? cchar:`char$();\> ? ? cshort:`short$();&gt; ? ? cint:`int$();\> ? ? cfloat:`float$();&gt; ? ? csym:`symbol$();\> ? ? time:`time$())&gt;&gt; startAud:`t 2:(`startAud; 1)\> startAud\> show all\_dt\> ^D\>\> Loading the script for a go with the code:\>\> q) \l t.q\> 0\> seq cchar cshort cint cfloat csym ? ? ? ?time\> -----------------------------------------------------\> 0 ? a ? ? 100 ? ?1000 99.9 ? hello world 00:00:00.200\> 1 ? a ? ? 100 ? ?1000 99.9 ? hello world 00:00:00.200\> 2 ? a ? ? 100 ? ?1000 99.9 ? hello world 00:00:00.200\>\> …seems to have done the trick.\>\> H?kan\</stdio.h\>\</lindqvist.h…\>
