cancel
Showing results for 
Search instead for 
Did you mean: 

64bit c++ app and 32kdb sever

oranjsr
New Contributor
Hi all,

Referencing "http://code.kx.com/wiki/Cookbook/InterfacingWithC", I was able to create the following table via a 64bit c++ app;

stime        rtime        sym bidprice bidsize askprice asksize 
-----------------------------------------------------------------------------..
06:14:25.847 06:14:25.882 ES  205175   69      205200   18
06:14:32.615 06:14:32.655 ES  205175   70      205200   18
06:14:33.265 06:14:33.305 ES  205175   71      205200   14
06:14:33.965 06:14:34.005 ES  205175   69      205200   19
06:14:38.015 06:14:38.055 ES  205175   69      205200   20
06:14:39.215 06:14:39.255 ES  205175   69      205200   21


However, when I try to access the data and manipulate it via c++ the following code does not work.  Is the code in error or is using a 64bit app the 32bit app not possible?

#include<cstdio>
#include<cstdlib>
#include"k.h"

// obtain k.h from http://kx.com/q/c/c/k.h
// linux:
//  compile with gcc -m64 -DKXVER=3 csv.c c.o
//  obtain c.o from http://kx.com/q/l64/c.o for linux
// windows:
//  start the x86 or 64 bit version of build environment, then: cl -DKXVER=3 /MD csv.c c.obj ws2_32.lib
//  obtain c.obj from http://kx.com/q/w32/ or w64/


int main(int argc,char*argv[])
{
int handle = khpu((S) "localhost", 5000, (S) "myusername:mypassword");

if (handle < 0)
{
printf("Cannot connect\n");
return 1;
}
else if (!handle)
{
printf("Wrong credentials\n");
return 1;
}

/*
  Get a keyed table by executing a query.
*/
K x = k(handle, "select from quote", (K)0);

/*
  Convert the result to a simple table.
  NB. x is no longer valid and has been deallocated.
*/
K y=ktd(x);

/*
  Note that if the ktd conversion fails for any reason,
  it returns 0 and x is not freed.
  since 2011-01-27, ktd always decrements ref count of input.
*/
if (0 == y)
   (void) puts ("x is still a keyed table because the conversion failed.");
else
   (void) puts ("y is a simple table and x has been deallocated.");

    K columnNames=kK(y->k)[0];
    K columnData=kK(y->k)[1];
    int nCols=columnNames->n; <<<<< cause seg fault
    int nRows=kK(columnData)[0]->n;
    for(int row=0;row<nRows;row++)
    {
        if(0==row)
        {
            for(int col=0;col<nCols;col++)
            {
                if(col>0)printf(",");
                printf("%s",kS(columnNames)[col]);
            }
            printf("\n");
        }
        for(int col=0;col<nCols;col++)
        {
            K obj=kK(columnData)[col];
            if(col>0)printf(",");
            switch(obj->t)
            {
                case(0):{ // handle a list of char vectors
                  K x=kK(obj)[row];
                  if(10==x->t){int i;for(i=0;i<xn;i++)printf("%c",kG(x)[i]);}
                  else printf("type %d not supported by this client",obj->t);
                }break;
                case(1):{printf("%d",kG(obj)[row]);}break;
                case(4):{printf("%d",kG(obj)[row]);}break;
                case(5):{printf("%d",kH(obj)[row]);}break;
                case(6):{printf("%d",kI(obj)[row]);}break;
                case(7):{printf("%lld",kJ(obj)[row]);}break;
                case(8):{printf("%f",kE(obj)[row]);}break;
                case(9):{printf("%f",kF(obj)[row]);}break;
                case(11):{printf("%s",kS(obj)[row]);}break;
                default:{printf("type %d not supported by this client",obj->t);}break;
            }
        }
        printf("\n");
    }

    kclose(handle);
    return 0;
}
 

Thanks,
Oran
5 REPLIES 5

charlie
New Contributor II
New Contributor II
Hi Oran,

the kdb+ ipc format is the same regardless of whether the sender/receiver is 32 or 64bit, so 32bit apps can communicate with 64bit apps.

Which OS/platform are you building for?
Are you using the correct c.o? e.g.
32bit for linux - kx.com/q/l32/c.o
64bit for linux - kx.com/q/l64/c.o

Did you define KXVER to be 3?
What is your compile cmd line? e.g. gcc example.c ....

Which of these did it print?
if (0 == y)
   (void) puts ("x is still a keyed table because the conversion failed.");
else
   (void) puts ("y is a simple table and x has been deallocated.");

For sanity checks you might also like to check the return value as in the example

  if(!result)
        printf("Network Error\n"),perror("Network"),exit(1);
    kclose(handle);
    if(result->t==-128)
        printf("Server Error %s\n",result->s),r0(result),exit(1);
    if(result->t!=99&&result->t!=98) // accept table or dict
        printf("type %d\n",result->t),r0(result),exit(1);

hth,
Charlie

Hi Charlie,

I defined KXVER to be 3 and it worked.

Thanks,
Oran

Hi Charlie,

Anyway around this?

type 19 not supported by this client,type 19 not supported by this client

Thanks,
Oran

charlie
New Contributor II
New Contributor II
Hi Oran,

type 19 is the time type. e.g.
q)type 10:00:00.000
-19h
q)type enlist 10:00:00.000
19h

I'll flesh out csv.c later but in the meantime you can derive the remaining cases from http://www.aquaq.co.uk/wp-content/uploads/2015/01/AquaQ-Analytics-Transferring-Data-between-kdb+-and...

thanks,
Charlie

Hi Charlie,

Thanks.

Oran