0

I am failing to create a list of strings in KDB C++ API. This is what I am doing:

 K lst = ktn(KC,0);
 jk(&lst, kp((S)"str_1")); 
 jk(&lst, kp((S)"str_2"));
 r1(lst);
 k(h, (S)"{show type x}" , lst, (K)0);
 r1(lst);
 k(h, (S)"{show count x}", lst, (K)0);
 r1(lst);
 k(h, (S)"{show x}"      , lst, (K)0);

The output

10h
2
"\260\260"

Where instead of "\260\260" could be some other random string repeated twice. It seems, I do create a list of strings, but the list contains rubbish for whatever reason. Could you please help me to understand what I am doing wrong here?

Thank you for your help!

1 Answer 1

1

You're initialising the list with type char (KC), so this will be a char list, not a string list.

A string list is a mixed type list, as it a list of list of chars in Kdb. Therefore, you need to initialise the list with type 0 (mixed list type).


Example C code - strList.c:

#define KXVER 3
#include "k.h"

K createStrListStatic(K x){
  K strList = ktn(0,2);
  kK(strList)[0] = kp("hello");
  kK(strList)[1] = kp("world");
  return strList;
}

K createStrListDynamic(K x){
  K strList = ktn(0,1);
  kK(strList)[0] = kp("hello");
  js(&strList, (S)kp("world"));
  return strList;
}

Example Q code to load the lib & call C funcs - strList.q:

createStrListStatic:`:strList 2:(`createStrListStatic;1);
createStrListDynamic:`:strList 2:(`createStrListDynamic;1);

-1 "\ncreateStrListStatic";
-1 "\t",.Q.s1 createStrListStatic[];
-1 "\ncreateStrListDynamic";
-1 "\t",.Q.s1 createStrListDynamic[];

Compile and run as follows:

$ gcc -shared -m32 -fPIC strList.c -o strList.so && LD_LIBRARY_PATH="." q strList.q
KDB+ 3.4 2016.10.27 Copyright (C) 1993-2016 Kx Systems
l32/ 8()core 7982MB salih glyph01 127.0.1.1 NONEXPIRE


createStrListStatic
        ("hello";"world")

createStrListDynamic
        ("hello";"world")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.